1001 Battle Over Cities - Hard Version (35 分)(C++)

PAT顶级题解目录​​​​​​​

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

题目大意:给出一些城市和城市之间的路径,有些路径需要维修。保证构成的图是连通的。现在如果去掉某个城市,要继续保证剩余城市构成连通图,那么就可能需要维修一些路径,现在要找到去除后维修代价最大的城市。最大代价的城市按顺序输出,如果都不要维修,则输出0。

解题思路:Kruskal最小树(并查集),依次遍历,求去除某个城市后,需要的维修代价,记录最大维修代价即可。

注意点:可能某个城市去掉后,永远无法构成连通图,则这个城市代价是最大的。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 505;
struct Edge
{
	int from, to, dis, flag;//分别为起点,终点,距离,是否要维修
	Edge(int a, int b, int c, int d): from(a), to(b), dis(c), flag(d){}
};
int n, m, a, b, d, f, father[N],cost;
vector<Edge>edges;
//寻找父亲节点,并进行路径压缩
int findFather(int x){//查找父亲结点并进行路径压缩
    if(x==father[x])
        return x;
    int temp=findFather(father[x]);
    father[x]=temp;
    return temp;
}
//计算连通分量的个数
int countFather(){
	int cnt = 0;
	for(int i = 1; i <= n; ++ i)
		if(father[i] == i)
			cnt += 1;
	return cnt;
}
int main(){
	std::vector<int> result;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < m; ++ i){
		scanf("%d %d %d %d", &a, &b, &d, &f);
		edges.push_back(Edge(a, b, d, f));
	}
	//排序的方式按不需要维修的路在前,距离小的路在前
	sort(edges.begin(), edges.end(), [](Edge a, Edge b){return a.flag == b.flag ? a.dis < b.dis : a.flag > b.flag;});
	int maxn = 0;
	for(int i = 1; i <= n; ++ i){
		for(int i = 1; i <= n; ++ i)
			father[i] = i;
		cost = 0;
		for(Edge e : edges){
			int ua = findFather(e.from), ub = findFather(e.to);
			if(ua != i && ub != i && ua != ub){
				cost += e.flag == 0 ? e.dis : 0;
				father[ua] = ub;
			}
		}
		//无法构成连通图
		if(countFather() > 2)
			cost = 0x3f3f3f3f;
		if(cost == maxn)
			result.push_back(i);
		else if(cost > maxn){
			result.clear();
		 	maxn = cost;
			result.push_back(i);
		}
	}
	if(maxn == 0)
		printf("0");
	else{
		printf("%d", result[0]);
		for(int i = 1; i < result.size(); ++ i)
			printf(" %d", result[i]);
	} 
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值