UVa12118 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.

1.题目大意:有V个城市,每两个城市之间有一条双向道路连接,长度均为T。给出一些边,现在我们需要求出经过这些指定的边最短的路径总长度

2.首先看到这个题,因为任何两个节点之间都有路相连,便直接求了连通块的个数,然后拿总边数加上了连通块的个数,WA了

3.仔细想想,如果一个连通块包含了如下的情况:
在这里插入图片描述
那么这个连通块只走三条边是走不完的。又思考了一会,还是没想出来是用欧拉通路来解决

4.看了网上,才知道要用欧拉通路来写,至于题目说任意两个节点都有边这个条件可以忽略,我们假设只有给出的边能走,走出一条欧拉通路,如果边不够再不断加边,这样就保证了是最短的欧拉通路。无向图的欧拉通路,无非是要么有两个度数为奇数的节点,要么度数全为偶数。还以上图为例,有四个奇数度数的节点,但是我们发现,只要在3-4或者2-4之间任意再添加一条边,就可以形成欧拉通路,再尝试几个例子,我们会发现这样一个规律,如果超过了两个度数为奇数的节点res,那么就需要添加(res-2)/2条边,这样再加上连通块个数减一,就是最后的总边数

这是第六章的最后一道题。翻了翻做紫书的第一道题的记录,距离现在已经七个月了,这六章我大概做了百分之七十的题,没做的是因为一些是太难,一些是模拟题,还有一些没搞懂暂时放着。我觉得自己有一定的提高,但是仍没有我想的那么明显。这学期安排自己课很少,我要快马加鞭,争取暑假前大概过一遍紫书。当后面的动态规划和数学做完后,是时候去CF打比赛了,暑假的训练也要更加认真。Fighting

代码:

#include <iostream>
#include <cstring>

using namespace std;
const int maxn=1005;
int G[maxn][maxn],degree[maxn];
bool vis[maxn];
int n,m,t,ans,res;

void dfs(int u){  //dfs求连通块,挺基础的,bfs或者并查集也行
    vis[u]=0;
    if(degree[u]&1) res++;
    for(int i=1;i<=n;i++){
        if(vis[i] && G[u][i]){
            dfs(i);
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int a,b,kase=0;
    while(~scanf("%d%d%d",&n,&m,&t)){
        if(n==0 && m==0 && t==0) break;
        memset(vis,0,sizeof vis);
        memset(G,0,sizeof G);
        memset(degree,0,sizeof degree);
        ans=0;
        for(int i=1;i<=m;i++){
            scanf("%d%d",&a,&b);
            G[a][b]=G[b][a]=1;  //无向图的邻接矩阵存法
            degree[a]++;degree[b]++;
            vis[a]=vis[b]=1;
            ans++;
        }
        for(int i=1;i<=n;i++){
            if(vis[i]){
                res=0;
                dfs(i);
                ans++;
                if(res>2) ans+=(res-2)/2; 
            }
        }
        if(ans) ans--; //连通块个数多加了一条边,特判是否为0防止出现负数
        printf("Case %d: %d\n",++kase,ans*t);
    }
    return 0;
}
发布了128 篇原创文章 · 获赞 7 · 访问量 5285

猜你喜欢

转载自blog.csdn.net/qq_44691917/article/details/104375718