UVa 12118 - Inspector's Dilemma(并查集,欧拉回路)

UVa 12118 - Inspector’s Dilemma(并查集,欧拉回路)

【vj链接】


题目

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


题目大意

一个人在城市间走,给出的这些路是必走的,其余任意点间也有路,走一条路要花时间T。问最少要多长时间能走完要求的所有路。


解题思路

一开始因为这道题放在搜索系列里就从dfs的角度想,无解。

但在走的过程中发现了一个关键点,如果走到一条死路的话,直接走到其他路的开始是绝对优于退回去再走的。还有如果有死路的话从死路开始走一定是最优的。

所以确定了走的思路,从一个度为奇数的点走到另一个度为奇数的点然后直接跳到下一个度为奇数的点这样,还是很难写,但是从这个可以联想到一笔画,七桥问题,欧拉回路balabala。

嗯,重点就是欧拉回路,对每一个连通图来说,如果有2*k个度为奇数的节点,那+k-1条边可以使它成为欧拉回路,如果有如果有2*k+1个度为奇数的节点,那+k-1条边也可以使它成为欧拉路径。

解题步骤:

1、求所有连通图和所有点的度(并查集);
2、对每个连通图求奇节点数(k),ans+=(k-2)/2使它成为欧拉路径;//不是k/2
3、ans加上连通图的数量-1,表示除了起点到每一个图需要一条边;
4、ans加上所有已有的边。

逻辑清晰,思维严谨然后wa了三发……
坑一:每条路要花费 t 时间;
坑二:可能没有连通图;
坑三:只有一个点的连通图不需要去;


AC代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn=1007;


int root[maxn];
int init(int x)
{
    for(int i=1;i<=x;i++)
        root[i]=i;
}
int find_root(int x)
{
    return root[x]=( x==root[x]? x:find_root(root[x]) );
}
bool join(int x,int y)
{
    x=find_root(x),y=find_root(y);
    if(x!=y)
    {
        root[y]=x;
        return 1;
    }
    return 0;
}
int d[maxn],nk[maxn],vis[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m,i,j,k,t,x,y,Case=0;
    while(1)
    {
        scanf("%d%d%d",&n,&m,&t);
        if(n==0&&m==0&&t==0) break;
        if(m==0)
        {
            printf("Case %d: %d\n",++Case,m);
            continue;
        }
        init(n);
        memset(d,0,sizeof(d));
        memset(nk,0,sizeof(nk));
        memset(vis,0,sizeof(vis));
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            join(x,y);
            d[x]++;
            d[y]++;
        }
        for(i=1;i<=n;i++)
        {
            vis[find_root(i)]++;
            if(d[i]%2) nk[find_root(i)]++;
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            //printf("vis[%d]=%d nk[%d]=%d\n",i,vis[i],i,nk[i]);
            if(vis[i]>1) ans++;
            if(nk[i]>2) ans+=nk[i]/2-1;
        }
        ans--;
        ans+=m;
        ans*=t;
        printf("Case %d: %d\n",++Case,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值