UVa 12118 - Inspector's Dilemma

In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directional of
course. A road-inspector’s task is to travel through the highways (in either direction) and to check if
everything is in order. Now, a road-inspector has a list of highways he must inspect. However, it might
not be possible for him to travel through all the highways on his list without using other highways. He
needs a constant amount of time to traverse any single highway. As you can understand, the inspector
is a busy fellow does not want to waste his precious time. He needs to know the minimum possible
time to complete his task. He has the liberty to start from and end with any city he likes. Please help
him out.
Input
The input file has several test cases. First line of each case has three integers: V (1 ≤ V ≤ 1000), the
number of cities, E (0 ≤ E ≤ V ∗ (V − 1)/2), the number of highways the inspector needs to check and
T (1 ≤ T ≤ 10), time needed to pass a single highway. Each of the next E lines contains two integers
a and b (1 ≤ a, b ≤ V , a ̸= b) meaning the inspector has to check the highway between cities a and b.
The input is terminated by a case with V = E = T = 0. This case should not be processed.
Output
For each test case, print the serial of output followed by the minimum possible time the inspector needs
to inspect all the highways on his list. Look at the output for sample input for details.
Sample Input
5 3 1
1 2
1 3
4 5
4 4 1
1 2
1 4
2 3
3 4
0 0 0
Sample Output
Case 1: 4
Case 2: 4

思路:这个题目的题意还是比较好懂,就是题目给出n个城市,和m条道路,以及每一条道路的花费,每一个城市都有通向剩余所有城市的道路,求经过所有m条道路的最小花费。这个题一开始想到的就是欧拉回路,但看别人的做法都是用的DFS判断的连通,但是感觉用并查集也可以做,在每一个连通块中计算奇数端点个数x,然后(x-1)/2就是额外需要走的道路,再加上每个连通块之间要走的道路与必须要走的道路就是走的最少的道路。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <algorithm>
#include <set>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>

#define MAXN 1100000
#define INF 0x3f3f3f3f
#define PI 3.14

using namespace std;

int n, m,t;

int rank1[MAXN];
int par[MAXN];
int vis[MAXN];
int sum[MAXN];

void init(int n) {
    for (int i = 1; i <= n; ++i) {
        rank1[i] = 0;
        par[i] = i;
    }
    memset(vis, 0, sizeof(vis));
    memset(sum, 0, sizeof(sum));
}

int find(int x) {
    if (par[x] == x)
        return x;
    else
        return par[x] = find(par[x]);
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
        return;
    if (rank1[x] < rank1[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rank1[y] == rank1[x])
            rank1[x]++;
    }
}

int main() {
    int kase = 0;
    while (cin >> n >> m >> t) {
        if (!n && !m && !t)
            break;
        int a, b;
        init(n);
        int ans = 0;
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            vis[a]++;
            vis[b]++;
            unite(a, b);
        }
        int cnt = 0;
        int cnt1 = 0;
        vector<int> vec;
        for (int i = 1; i <= n; ++i) {
            if (vis[i] % 2 == 1) {
                sum[find(i)]++;
            }
            if (vis[i] && par[i] == i) {
                vec.push_back(i);
            }
        }
        for (int i = 0; i < vec.size(); ++i) {
            cnt += (sum[vec[i]] - 1) / 2;
            cnt1++;
        }
        if (cnt1 > 0)
            cnt += (cnt1 - 1);
        cout << "Case " << ++kase << ": " << m * t + cnt * t << endl;
    }
    return 0;
}

再附上一个DFS做法的代码

#include<iostream>  
#include<vector>  
#include<cstring>  
using namespace std;  
const int maxn=1010;  
int v,e,t,cnt;  
bool vis[maxn];  
vector<int> g[maxn];  
bool read(){  
    cin>>v>>e>>t;  
    if(!v&&!e&&!t) return false;  
    for(int i=0;i<maxn;++i)  
        g[i].clear();  
    memset(vis,0,sizeof(vis));  
    for(int i=0;i<e;++i){  
        int a,b;  
        cin>>a>>b;  
        g[a].push_back(b);  
        g[b].push_back(a);  
    }  
    return true;  
}  
void dfs(int cur){  
    if(vis[cur]) return;  
    vis[cur]=true;  
    cnt+=(int)g[cur].size()&1;//统计度数为奇数的点。  
    for(int i=0;i<g[cur].size();++i)  
        dfs(g[cur][i]);//DFS求连通。  
    return;  
}  
int solve(){  
    int ans=0;  
    for(int i=1;i<=v;++i){  
        if(!vis[i]&&!g[i].empty()){  
            cnt=0;  
            dfs(i);  
            ans+=max(cnt,2);//每条道路至少需要2个端点,防止出现回路出错。  
        }  
    }  
    return t*(max(ans/2-1,0)+e);//之前求得的和减去初始值任选可以减少的一次,再加上必经的e条道路。  
}  
int main(){  
    ios::sync_with_stdio(false);  
    int k=0;  
    while(read())  
        cout<<"Case "<<++k<<": "<<solve()<<endl;  
    return 0;  
}  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值