Inspector's Dilemma UVA - 12118

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1e3+5;
int n,m,a,b,i,T,temp;
vector<int > vec[maxn];
//判断有多少条无向欧拉通路(x1)和多少条欧拉回路(x2)
//有路就有通路除非 m = 0
//if(x1==0 && x2 == 1) m条
//其他  (x1+x2-1)+m

//实现
//dfs找奇数点,通路有两个(一条通路),回路没有,就是一条回路
int vis[maxn];
void dfs(int x)
{
    if(!vis[x]) return; //走过的点
    vis[x] = 0;
    temp+=(vec[x].size() & 1);
    for(int j = 0; j < vec[x].size(); j++)
        if(vis[vec[x][j] ])
            dfs(vec[x][j]);
}
int main()
{
    int cnt = 1;
    while(scanf("%d%d%d",&n,&m,&T)&&(n||m||T))
    {
        memset(vis,1,sizeof vis);
        for(i = 1; i <= n; i++)
            vec[i].clear();
        for(i = 0; i < m; i++)
        {
            scanf("%d %d",&a,&b);
            vec[a].push_back(b);
            vec[b].push_back(a);
        }
        int sum = 0;
        for(i = 1; i <= n; i++)
            if(vis[i]&&!vec[i].empty())
            {
                temp = 0;
                dfs(i);
                sum += max(2,temp);
            }
       printf("Case %d: %d\n",cnt++,T*(max(sum/2-1,0)+m) );
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Nothing_227/article/details/81747787