其他分享
首页 > 其他分享> > 7-19 Battle Over Cities - Hard Version (35分)

7-19 Battle Over Cities - Hard Version (35分)

作者:互联网

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

给出一个连通图,其中有些边是正常的status为1,有一些是损坏的status为0,如果要用到损坏的边那就要花费代价,正常的边不用花费,如果一个城市沦陷,问其余城市要保持连通的花费,哪个城市或者哪几个城市沦陷花费最大。如此先去掉沦陷的城市,然后用正常的边看看其他城市是否连通,连通则以,如果不连通那么就要用损坏的边来弥补,当然是先用花费小的,所以要先排序,正常边在前,损坏的在后,各自按花费再排好序。如果加上损坏的边都不能连通,这种情况是需要更多花费的,不要遗漏。
类似最小生成树的kruskal算法。
代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;

int n,m;
struct edge {
    int x,y,c,s;
}e[250000];
int f[550];
bool cmp(edge &a,edge &b) {
    if(a.s != b.s) return a.s > b.s;
    return a.c < b.c;
}
inline void init() {
    for(int i = 1;i <= n;i ++) {
        f[i] = i;
    }
}
inline int getf(int k) {
    if(f[k] == k) return f[k];
    return f[k] = getf(f[k]);
}
int main() {
    int ma = 1,ans[550],cnt = 0;
    scanf("%d%d",&n,&m);
    for(int i = 0;i < m;i ++) {
        scanf("%d%d%d%d",&e[i].x,&e[i].y,&e[i].c,&e[i].s);
    }
    sort(e,e + m,cmp);
    for(int i = 1;i <= n;i ++) {
        init();
        int c = 0,num = 0;
        for(int j = 0;j < m;j ++) {
            if(num == n - 2) break;
            if(e[j].x == i || e[j].y == i) continue;
            int a = getf(e[j].x),b = getf(e[j].y);
            if(a == b) continue;
            f[a] = b;
            num ++;
            if(!e[j].s) c += e[j].c;
        }
        if(num != n - 2) c = inf;
        if(c > ma) ma = c,ans[0] = i,cnt = 1;
        else if(c == ma) ans[cnt ++] = i;
    }
    for(int i = 0;i < cnt;i ++) {
        if(i) putchar(' ');
        printf("%d",ans[i]);
    }
    if(!cnt) printf("0");
}

 

标签:case,city,19,Over,Hard,highways,int,cities,highway
来源: https://www.cnblogs.com/8023spz/p/12270825.html