1001. Battle Over Cities - Hard Version (35)

       听了许多学长们的意见,都建议不要落下数据结构和算法这一块。于是作为一个非CS的学生,打算陷入PAT顶级的刷题里。希望能够补充点数据结构的知识,写一点心得,免得让自己忘得的太快。

第一题链接: https://www.patest.cn/contests/pat-t-practise/1001

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

题解:大体意思就是讲,失去一个城市后,要将剩余的城市连通,所需的重建道路代价(cost)为衡量标准,求出在一个无向连通图中(题目保证一定连通),最重要的城市(cost最大)。

输出结果有两种,1是没有所谓的最重要的城市,即所有城市cost为0,则输出0;2是输出cost最大,若相同则按节点升序输出。

参考https://blog.csdn.net/jtjy568805874/article/details/50759425

方案:求一个城市的重要性,可以演化成将剩余城市联通,所需的cost最大,代表该城市就越重要。将图演变 成最小生成树(MST)。可以用Kruskal算法和Prim算法。

这边采用Kruskal算法:

 

  1. 在一个G(V,E)的网络中,去除所有边,将n个顶点视为n颗树。
  2. 在set{edge(u,v)}边的集合中,选择一条权值最小的边e(u,v),此时连同节点u,v,使得剩余n-1颗树。
  3. 不断重复上述过程2,使得最终只剩下1颗树。此时,必然采用n-1条边。

针对上述题,首先对边进行排序,除了cost按升序排列,status状态值为1的权值小于status状态值为0的。若最终无法成为连通图,则该节点重要性为无穷大(判断条件即为,num_of_edge < n-1)以下为AC代码:

#include<iostream>
#include<algorithm>
#include<set>
#include<memory>
#include<iterator>
#include<limits.h>
#include<vector>
#include<math.h>
using namespace std;
set<int> s = {};
int need[501];
int fa[501];
int get(int x)
{
	return x == fa[x] ? x : fa[x] = get(fa[x]);
}
struct edge
{
	int u, v;
	int cost, status;
	void set(int u, int v, int cost, int status)
	{
		this->u = u;
		this->v = v;
		this->cost = cost;
		this->status = status;
	}
};
bool cmp (const edge & q ,const edge &p)
{
	if (p.status == q.status)
		return q.cost < p.cost;
	else
		return q.status > p.status;
}
vector<edge> edges;
int main()
{
	int n, m;
	cin >> n >> m;
	int i = 1;
	while (i <= m)
	{
		int v, u, c, s;
		//scanf("%d%d%d%d", &v, &u, &c, &s);
		cin >> u >> v >> c >> s;
		edge e;
		e.set(u, v, c, s);
		edges.push_back(e);
		i++;
	}
	sort(edges.begin(), edges.end(),cmp);
	int min = 0;
	for (int i = 1; i <= n; i++)
	{
		s.clear();
		for (int j = 1; j <= n; j++) fa[j] = j;
		int edge_num = 0;
		for (int j = 0; j < m; j++)
		{
			if ((edges[j].u == i || edges[j].v == i )) continue;
			//if (s.find(edges[j].u) != s.end() && s.find(edges[j].v) != s.end()) continue;
			//s.insert(edges[j].u); s.insert(edges[j].v);
			int fx = get(edges[j].u), fy = get(edges[j].v);
			if (fx == fy) continue;
			edge_num++;
			fa[fx] = fy;
			if (edges[j].status == 0) need[i] += edges[j].cost;
			if (s.size() == n - 1) break;
		}
		if (edge_num < n - 2)
			need[i] = INT_MAX;
		min = max(min, need[i]);
	}

	bool flag = false;
	if (min == 0) cout << "0";
	else
	{
		for (int i = 1; i <= n; i++)
		{
			if (need[i] == min)
			{
				if (flag) cout << " ";
				cout << i;
				flag = true;
			}
		}
	}
	return 0;
}

Tips:起初想用set的思维,发现最后两个测试点超时。另外,这里scanf与cin还是有差距的,可以通过c++优化的方式。

在set上踩了坑,所以了解了一下set,具体可以参见我的blog

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值