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;
}
相关推荐
JNU freshman19 分钟前
力扣第435场周赛讲解
算法·leetcode·蓝桥杯
眼镜哥(with glasses)20 分钟前
蓝桥杯python基础算法(2-2)——基础算法(B)——模拟(上)
算法
赵鑫亿2 小时前
7.DP算法
算法·dp
iqay2 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
还有糕手2 小时前
算法【有依赖的背包】
算法·动态规划
pursuit_csdn3 小时前
力扣 347. 前 K 个高频元素
算法·leetcode
wen__xvn3 小时前
每日一题洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵c++
c++·算法·矩阵
makabaka_T_T3 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
辞半夏丶北笙4 小时前
最近最少使用算法(LRU最近最少使用)缓存替换算法
java·算法·缓存
BingLin-Liu4 小时前
蓝桥杯备考:六大排序算法
算法·排序算法