zoukankan      html  css  js  c++  java
  • UVA12188-Inspector's Dilemma(欧拉回路+连通性判断)

    Problem UVA12188-Inspector's Dilemma

    Time Limit: 3000 mSec

    Problem Description

    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 Ouput

    Case 1: 4

    Case 2: 4

    题解:第一次感到用vector实现邻接表比用链式前向星在有的方面好一些。

    原来因为效率原因基本不用vector实现,这个题中需要统计度数所以用vector就很方便了,否则还要遍历连出去的边,计数,相对麻烦。

    言归正传,这个题思路不难想到,根据题中给出的边,图中形成了一个个联通块,这道题明显是欧拉路径的情况下最短,统计每一个联通块中度数为奇数的点的个数n,这样的点都需要构造一条边使其度数为偶数,最终要的是欧拉路径而不是回路,因此所有联通块的奇度数点-2是需要构造边的点,这些点之间连边,再加上必须的e条即为答案。

     1 #include <iostream>
     2 #include <vector>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cstdlib>
     6 using namespace std;
     7 
     8 const int maxn=1010;
     9 
    10 int v,e,t,cnt;
    11 bool vis[maxn];
    12 vector<int> gra[maxn];
    13 
    14 void dfs(int cur){
    15     if(vis[cur]) return;
    16     vis[cur]=true;
    17     cnt += (int)gra[cur].size()&1;
    18     for(int i = 0;i < gra[cur].size();i++){
    19         dfs(gra[cur][i]);
    20     }
    21     return;
    22 }
    23 
    24 int solve(){
    25     int ans=0;
    26     for(int i=1;i<=v;++i){
    27         if(!vis[i] && !gra[i].empty()){
    28             cnt=0;
    29             dfs(i);
    30             ans+=max(cnt,2);
    31         }
    32     }
    33     return t*(max(ans/2-1,0)+e);
    34 }
    35 
    36 int iCase = 1;
    37 
    38 int main()
    39 {
    40     //freopen("input.txt","r",stdin);
    41     while(scanf("%d%d%d",&v,&e,&t) && (v||e||t)){
    42         for(int i = 1;i <= v;i++){
    43             gra[i].clear();
    44         }
    45         memset(vis,0,sizeof(vis));
    46         for(int i=0;i<e;++i){
    47             int a,b;
    48             scanf("%d%d",&a,&b);
    49             gra[a].push_back(b);
    50             gra[b].push_back(a);
    51         }
    52         printf("Case %d: %d
    ",iCase++,solve());
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    QT实现软件重启
    Qt 延时
    gcc 创建库及使用
    verilog 奇数分频设计
    内核中的 likely() 与 unlikely()
    TFT LCD 参数详解
    手动安装m4, autoconf, automake, libtool
    [其他] 蒙特卡洛(Monte Carlo)模拟手把手教基于EXCEL与Crystal Ball的蒙特卡洛成本模拟过程实例:
    Origin9.1如何绘制风向玫瑰图(Binned Data)?
    Origin9.1如何使用原始数据(Raw Data)绘制风向玫瑰图
  • 原文地址:https://www.cnblogs.com/npugen/p/9533010.html
Copyright © 2011-2022 走看看