12118 - Inspector's Dilemma

结题思路:   首先只管需要经过的边,这样得到一个可能具有几个连通块的图,注意题中任意两点之间都有一条边。这样要想走完所有的边,则必须加上一定量的边使得图变成连通图,这个边数就是连通块的个数t-1 ; 然后对于每个连通块,只要加上一定的边使其变成哈密顿图就可以了,在此题中就是统计每个连通块中的边数(这里使用连通块点的度数之和就可以获得),以及奇数点的个数。这样就可以根据哈密顿图的条件,消灭一定量的奇数度的点(消灭的方法就是加边)。 需要加的边数加上原来连通图的边数就是观察员在每个每个连通块需要走的边数。



//  main.cpp
//  uva 12118 Inspector’s Dilemma
//
//  Created by XD on 15/7/19.
//  Copyright (c) 2015年 XD. All rights reserved.
//

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;

int flag[1010] ;

vector<int>   g[1010] ;
int d[1010]  ;
set<int> node ;
int asn ;
int conn ;
int v , e,t ;
int bfs(int x )
{
    if (flag[x]==1) {
        return 0   ;
    }
    conn++ ;
     int d1= 0 ;
    queue<int >   q  ;
    flag[x] = 1 ;
    if (d[x]%2==1) {
         d1++ ;
    }    q.push(x) ;
    int s ,to;
    ;
    int d2 = d[x] ;
    while (!q.empty()) {
        s = q.front()  ; q.pop() ;
        long len = g[s].size() ;
        for (int i = 0 ; i < len; i++) {
            to = g[s][i] ;
            if (flag[to]== 0) {
                d2 += d[to] ;
                flag[to] = 1;
                q.push(to) ;
                if (d[to]%2==1) {
                    d1++ ;
                }
            }
        }
    }
    if (d1==2 ||d1== 0) {
        return  d2/2 ;  ;
    }
    else
    {
        return  d2/2+ (d1 - 2)/2  ;
    }
}
int main() {
    
    int  x , y  ;
    int casenum = 0 ;
    while (scanf("%d%d%d" ,&v ,&e,&t)==3 && v + e + t != 0 )
    {
        node.clear() ;
        asn = 0   ;
        conn = 0 ;
        memset(flag, 0, sizeof(flag)) ;
        memset(d, 0, sizeof(d)) ;
        for (int i  = 0; i <= v; i++) {
            g[i].clear() ;
        }
        for (int i = 0 ; i < e; i++) {
            scanf("%d%d" , &x,&y) ;
            g[x].push_back(y) ;
            g[y].push_back(x) ;
            d[x]++ ; d[y]++ ;
            node.insert(x) ;
            node.insert(y) ;
        }
        for (set<int >::iterator it = node.begin() ; it!= node.end()  ;it++) {
          
            if (flag[*it] == 1) {
                continue  ;
            }
            asn += bfs(*it) ;
        }
        int temp = 0 ;
        if (conn !=0 ) {
            temp = 1 ;
        }
        printf("Case %d: %d\n" , ++casenum,  (asn + conn - temp)*t) ;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值