• Battle Over Cities


    It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.

    Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤500), and M, which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers: City1 City2 Cost Status where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.

    Note: It is guaranteed that the whole country was connected before the war.

    Output Specification:

    For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.

    In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.

    Sample Input 1:

    4 5
    1 2 1 1
    1 3 1 1
    2 3 1 0
    2 4 1 1
    3 4 1 0
    
     

    Sample Output 1:

    1 2
    
     

    Sample Input 2:

    4 5
    1 2 1 1
    1 3 1 1
    2 3 1 0
    2 4 1 1
    3 4 2 1
    
     

    Sample Output 2:

    0
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define MAX 300000
     4 #define INF 0x7fffffff
     5 int root[505],costs[505];
     6 struct route{
     7     int c1,c2,cost,status;
     8     void read(){scanf("%d%d%d%d",&c1,&c2,&cost,&status);}
     9     //重载小于,好的路径在前,cost递增
    10     bool operator <(const route& r){return r.status==status?cost<r.cost:status>r.status;}
    11 }rou[MAX];
    12  
    13 int getroot(int n)  //返回集合的根结点,并将路径上节点的父亲节点修改为根节点。
    14 {
    15     return root[n]==n?n:root[n]=getroot(root[n]);
    16 }
    17  
    18 int main()
    19 {
    20     //freopen("test.txt","r",stdin);
    21     int N,M,i,j,num;
    22     scanf("%d%d",&N,&M);
    23     for(i=0;i<M;++i)rou[i].read();
    24     sort(rou,rou+M);
    25     int ans=0;
    26     for(i=1;i<=N;++i){
    27         for(j=1;j<=N;++j)root[j]=j;
    28         costs[i]=0;num=N-2;//M-2条路
    29         for(j=0;j<M;++j){
    30             if(rou[j].c1==i||rou[j].c2==i)continue;//不计连接该点的所有路径
    31             int r1=getroot(rou[j].c1),r2=getroot(rou[j].c2);
    32             if(r1==r2)continue;//已经属于一个集合
    33             --num;  //集合个数减一
    34             root[r2]=r1;//合并两个集合
    35             if(!rou[j].status)costs[i]+=rou[j].cost;
    36         }
    37         if(num>0)costs[i]=INF;
    38         ans=max(ans,costs[i]);
    39     }
    40     if(ans){
    41         bool flag=0;
    42         for(i=1;i<=N;++i){
    43             if(costs[i]==ans){
    44                 printf("%s%d",flag?" ":"",i);
    45                 flag=1;
    46             }
    47         }
    48         return 0;
    49     }
    50     printf("0");
    51     return 0;
    52 }
    诚者,君子之所守也。
  • 相关阅读:
    敏捷结果30天之第十七天:找出高效时间,并利用它来处理重要事情
    每天一个linux命令(13):less 命令
    敏捷结果30天之第十九天:你在为谁做事
    每天一个linux命令(6):rmdir 命令
    每天一个linux命令(11):nl命令
    代码重构学习笔记三:重构72招式
    每天一个linux命令(4):mkdir命令
    敏捷结果30天之第二十二天:设计你的一天
    敏捷结果30天之第二十一天:正面失败,吸取教训,改善结果
    每天一个linux命令(8):cp 命令
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285827.html
Copyright © 2020-2023  润新知