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;
}
相关推荐
二年级程序员20 分钟前
单链表算法题思路详解(上)
c语言·数据结构·c++·算法
wshzd30 分钟前
LLM之Agent(四十四)|使用 GRPO 算法训练多智能体系统用于复杂任务规划
算法
谁不学习揍谁!1 小时前
基于python机器学习算法的农作物产量可视化分析预测系统(完整系统源码+数据库+详细文档+论文+详细部署教程+答辩PPT)获取方式
python·算法·机器学习
ADDDDDD_Trouvaille2 小时前
2026.2.13——OJ75-77题
c++·算法
重生之后端学习2 小时前
230. 二叉搜索树中第 K 小的元素
java·数据结构·算法·深度优先
近津薪荼2 小时前
dfs专题7—— 全排列
c++·学习·算法·深度优先
你的冰西瓜2 小时前
C++ STL算法——非修改序列算法
开发语言·c++·算法·stl
闻缺陷则喜何志丹2 小时前
P12275 [蓝桥杯 2024 国 Python B] 工厂|普及+
c++·算法·蓝桥杯·洛谷
宝贝儿好2 小时前
【强化学习】第九章:基于Action-Critic框架的强化学习
人工智能·python·深度学习·算法·动态规划
laplace01232 小时前
KL 散度1
人工智能·算法·agent·qwen