PAT-Top1001. Battle Over Cities - Hard Version (35)
在敌人占领之前由城市和公路构成的图是连通图。在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通。修复的代价越大,意味着这个城市越重要。如果剩下的城市无法互通,则说明代价无限大,这个城市至关重要。最后输出的是代价最大的城市序号有序列表。借助并查集和Kruskal算法(最小生成树算法)来解决这个问题。
//#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; struct edge { // edge struct
int u, v, cost;
};
vector<edge> edges; // the number of edges is greater than 500 far and away int cmp(edge a, edge b) { // sort rule
return a.cost < b.cost;
} int parent[]; // union-find set void initParent(int n) { // initialize union-find set
int i;
for(i = ; i <= n; i++) {
parent[i] = -; // a minus means it is a root node and its absolute value represents the number of the set
}
} int findRoot(int x) { // find the root of the set
int s = x;
while(parent[s] > ) {
s = parent[s];
} int temp;
while(s != x) { // compress paths for fast lookup
temp = parent[x];
parent[x] = s;
x = temp;
} return s;
} void unionSet(int r1, int r2) { // union sets. More concretely, merge a small number of set into a large collection
int sum = parent[r1] + parent[r2];
if(parent[r1] > parent[r2]) {
parent[r1] = r2;
parent[r2] = sum;
} else {
parent[r2] = r1;
parent[r1] = sum;
}
} int maxw = ; // max cost
bool infw; // infinite cost int kruskal(int n, int m, int out) { // Kruskal algorithm to get minimum spanning tree
initParent(n); int u, v, r1, r2, num = , i, w = ;
for (i = ; i < m; i++) {
u = edges[i].u;
v = edges[i].v; if (u == out || v == out) {
continue;
} r1 = findRoot(u);
r2 = findRoot(v); if (r1 != r2) {
unionSet(r1, r2);
num++; if (edges[i].cost > ) { // only consider the cost which is not zero
w += edges[i].cost;
} if (num == n - ) {
break;
}
}
} //printf("num %d\n", num);
if (num < n - ) { // spanning tree is not connected
w = -; // distinguish the situation of the occurrence of infinite cost if (!infw) { // when infinite cost first comes out
infw = true;
}
} return w;
} int main() {
int n, m;
scanf("%d%d", &n, &m); int i, status;
edge e;
for (i = ; i < m; i++) {
scanf("%d%d%d%d", &e.u, &e.v, &e.cost, &status);
if (status == ) {
e.cost = ;
} edges.push_back(e);
} if (m > ) {
sort(edges.begin(), edges.end(), cmp);
} int curw, res[], index = ;
for (i = ; i <= n; i++) { // traverse all vertices to obtain the target vertex
curw = kruskal(n, m, i);
if (!infw) { // when infinite cost doesn't come out
if (curw < maxw) {
continue;
} if (curw > maxw) {
index = ;
maxw = curw;
}
res[index++] = i;
} else { // otherwise
if (curw < ) {
if (maxw > ) {
maxw = -;
index = ;
} res[index++] = i;
}
}
} if (index > ) {
for (i = ; i < index; i++) {
if (i > ) {
printf(" ");
}
printf("%d", res[i]);
}
} else {
printf("");
}
printf("\n"); system("pause");
return ;
}

参考资料
pat-top 1001. Battle Over Cities - Hard Version (35)
PAT-Top1001. Battle Over Cities - Hard Version (35)的更多相关文章
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- PAT 1013 Battle Over Cities (dfs求连通分量)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT 1013 Battle Over Cities DFS深搜
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- pat1001. Battle Over Cities - Hard Version 解题报告
/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+ ...
- 「日常训练」Battle Over Cities - Hard Version(PAT-TOP-1001)
题意与分析 题意真的很简单,实在不想讲了,简单说下做法吧. 枚举删除每个点,然后求最小生成树,如果这个路已经存在那么边权就是0,否则按照原来的处理,之后求花费,然后判整个图是否联通(并查集有几个roo ...
- PAT_A1013#Battle Over Cities
Source: PAT A1013 Battle Over Cities (25 分) Description: It is vitally important to have all the cit ...
随机推荐
- 让simplejson支持datetime类型的序列化
simplejson是Python的一个json包,但是觉得有点不爽,就是不能序列化datetime,稍作修改就可以了: 原文:http://blog.csdn.net/hong201/article ...
- python下载想听的有声书,让喜马拉雅收费,我是程序员!
from urllib import parse,request import urllib header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT ...
- Redis的并发竞争问题
问题描述:多客户端同时并发写一个key,可能本来应该先到的数据后到了,导致数据版本错了.或者是多客户端同时获取一个key,修改值之后再写回去,只要顺序错了,数据就错了. 一个key的值是1,本来按顺序 ...
- jquery.pjax 单页面, 无刷新打开页面.
介绍: pushState+ajax=pjax 工作原理: 什么是pjax? 现在很多网站(facebook, twitter)都支持这样的一种浏览方式, 当你点击一个站内的链接的时候, 不是做页面跳 ...
- python全栈开发day56-mysql
1.数据库和表 show总结 SHOW DATABASES;返回可用数据库的一个列表. SHOW TABLES;返回当前选择的数据库内可用表的列表. SHOW COLUMNS FROM custome ...
- JMeter3.0 post参数/BeanShell中文乱码问题
在用JMeter,在http请求的 Body Data或BeanShell中写的中文,为什么都是乱码—都是方框中间有个问号. 而且字体非常小,看着吃力,乱码现象如下图:
- mysql主从复制(半同步方式)
mysql主从复制(半同步方式) 博客分类: MySQL mysqlreplication复制 一.半同步复制原理介绍 1. 优点 当事务返回客户端成功后,则日志一定在至少两台主机上存在. MySQ ...
- 请使用千位分隔符(逗号)表示web网页中的大数字
方法一:使用正则表达式 语法如下: String(Number).replace(/(\d)(?=(\d{3})+$)/g, "$1,"); 举例: String(12345678 ...
- html5的audio实现高仿微信语音播放效果Demo
HTML部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- BZOJ3052/UOJ#58 [wc2013]糖果公园 莫队 带修莫队 树上莫队
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3052.html 题目传送门 - BZOJ3052 题目传送门 - UOJ#58 题意 给定一棵树,有 ...