12118 - Inspector‘s Dilemma (UVA)

题目链接如下:

Online Judge

脑雾严重,这道题一开始我想的方向有问题.....后来看了别人的题解才写出来的.....

用的是欧拉路径的充要条件;以及数连通块。需要加的高速路数目 = 连通块个数 - 1 + sum(每个连通块中连成欧拉路径需要加的高速路数目)。

cpp 复制代码
#include <cstdio>
#include <algorithm>
// #define debug

int V, E, T, a, b, tot, odd, kase = 0;
int arc[1001][1001];
bool vis[1001];
int cnt[1001];

void dfs(int k){
    vis[k] = true;
    if (cnt[k] % 2){
        odd++;
    }
    for (int i = 1; i <= V; ++i){
        if (!vis[i] && arc[i][k]){
            dfs(i);
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d %d %d", &V, &E, &T) == 3 && (V || E || T)){
        std::fill(vis, vis + V + 1, true);
        std::fill(cnt, cnt + V + 1, 0);
        tot = E - 1;
        for (int i = 0; i <= V; ++i){
            for (int j = 0; j <= V; ++j){
                arc[i][j] = arc[j][i] = 0;
            }
        }
        for (int i = 0; i < E; ++i){
            scanf("%d %d", &a, &b);
            arc[a][b] = arc[b][a] = 1;
            vis[a] = vis[b] = false;
            cnt[a]++;
            cnt[b]++;
        }
        for (int i = 1; i <= V; ++i){
            if (!vis[i]){
                odd = 0;
                dfs(i);
                if (odd > 2){
                    tot += (odd - 2) / 2;
                }
                tot++;
            }
        }
        printf("Case %d: %d\n", ++kase, E == 0 ? 0 : tot * T);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
YuTaoShao7 分钟前
【LeetCode 每日一题】36. 有效的数独
linux·算法·leetcode
IT古董11 分钟前
【漫话机器学习系列】003.Agglomerative聚类
人工智能·算法·机器学习
zstar-_21 分钟前
【不背八股】12.十大排序算法
数据结构·算法·排序算法
吃着火锅x唱着歌28 分钟前
LeetCode 2110.股票平滑下跌阶段的数目
数据结构·算法·leetcode
疋瓞1 小时前
C++_STL和数据结构《1》_STL、STL_迭代器、c++中的模版、STL_vecto、列表初始化、三个算法、链表
数据结构·c++·算法
JJJJ_iii1 小时前
【左程云算法09】栈的入门题目-最小栈
java·开发语言·数据结构·算法·时间复杂度
Bear on Toilet2 小时前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
金融小师妹2 小时前
多因子AI回归揭示通胀-就业背离,黄金价格稳态区间的时序建模
大数据·人工智能·算法
程序员东岸2 小时前
C语言入门指南:字符函数和字符串函数
c语言·笔记·学习·程序人生·算法
小猪咪piggy2 小时前
【算法】day2 双指针+滑动窗口
数据结构·算法·leetcode