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

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]);
	} 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章