pat-top 1001 Battle Over Cities - Hard Version (最小生成树)

1001 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

解题思路

 循环,每次循环计算去掉第i个点的时候的,求剩下的点的最小生成树。在存边的时候把原来完好的路的权值记为0,因为当一个城市被攻陷的时候,这个城市将关闭,所以连接这个城市的路就用不到了,其他好的也不用修。然后求各个城市最小生成树的权值最大的升序输出。还有要注意的是,如果一个城市被攻陷后,剩下的就不能不能联通了,那就相当于无限花费,是最大值了。

代码如下

#include <iostream>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f 
using namespace std;
struct Line{
	int l, r, w;
	Line(int l, int r, int w): l(l), r(r), w(w){	}
};
vector<Line> line;
int parent[505];
bool cmp(Line a, Line b)
{
	return a.w < b.w;
}
int f(int p)
{
	if(p == parent[p])
		return p;
	else 
		return parent[p] = f(parent[p]);
}
int main()
{
	int n, m;
	while(cin >> n >> m){
		for(int i = 0; i < m; i ++){
			int x, y, w, s;
			cin >> x >> y >> w >> s;
			if(s)
				w = 0;
			line.push_back(Line(x, y, w));
		}
		sort(line.begin(), line.end(), cmp);
		int cost[505] = {0};
		int maxx = 0;
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= n; j ++)
				parent[j] = j;
			int c = 0;
			int num = 0;   //记连上了几条边 
			for(int j = 0; j < line.size(); j ++){
				if(line[j].l == i || line[j].r == i)
					continue;
				int l = f(line[j].l);
				int r = f(line[j].r);
				if(l != r){
					parent[l] = r;
					c += line[j].w;
					num ++;
				}	
			}
			if(num < n - 2)  //n - 1个顶点,n-2条边 
				c = INF;
			cost[i] = c;
			//cout << "c " << c << endl;
			if(c > maxx)
				maxx = c;
		}
		if(maxx == 0)
			cout << 0 << endl;
		else {
			bool first = true;
			for(int i = 1; i <= n; i ++){
				if(cost[i] == maxx){
					if(first)
						first = false;
					else
						cout << " ";
					cout << i;
				}
			}
			cout << endl;
		}
		line.clear();
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值