在敌人占领之前由城市和公路构成的图是连通图。在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通。修复的代价越大,意味着这个城市越重要。如果剩下的城市无法互通,则说明代价无限大,这个城市至关重要。最后输出的是代价最大的城市序号有序列表。借助并查集和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)的更多相关文章

  1. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  2. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  3. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. pat1001. Battle Over Cities - Hard Version 解题报告

    /**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+ ...

  8. 「日常训练」Battle Over Cities - Hard Version(PAT-TOP-1001)

    题意与分析 题意真的很简单,实在不想讲了,简单说下做法吧. 枚举删除每个点,然后求最小生成树,如果这个路已经存在那么边权就是0,否则按照原来的处理,之后求花费,然后判整个图是否联通(并查集有几个roo ...

  9. PAT_A1013#Battle Over Cities

    Source: PAT A1013 Battle Over Cities (25 分) Description: It is vitally important to have all the cit ...

随机推荐

  1. Scrapy 框架 安装

    Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...

  2. Android自定义View+贝赛尔曲线

    Android -- 贝塞尔曲线公式的推导和简单使用https://www.cnblogs.com/wjtaigwh/p/6647114.html Android -- 贝塞尔使圆渐变为桃心http: ...

  3. 使用python解决算法和数据结构--使用栈实现符号匹配

    现在要自己来实现这些数据结构和常用算法了. 把基础再打牢一些. 栈的作用很大,无须多言. 我尽量看了题目要求,自己来实现代码的. # coding = utf-8 class Stack: def _ ...

  4. [转] mongoose 之Shema

    总 之见到我写点啥简直是奇迹,由于现在喜欢上玩转node.js +mongoose,个人爱好,靠近前端的又是英文文档,苦逼得很,悟出来一个写一个吧.之前喜欢误打误撞,网上搜索一点解决一下问题,后来实在 ...

  5. NEST - Elasticsearch 的高级客户端

    NEST - High level client Version:5.x 英文原文地址:NEST - High level client 个人建议:学习 NEST 的官方文档时,按照顺序进行,不宜跳来 ...

  6. jQuery中live函数的替代-【jQuery】

    在老版本的jQuery中,当需要对页面上某个由ajax加载的某片段的页面内容响应事件时,可以使用live函数来响应其事件,比如: $('a').live('click', function() { b ...

  7. js前端ajax提交list集合参数至后端

    var orderNosList = new Array(); var rows = $("#dg_linkOrder").datagrid("getChecked&qu ...

  8. Codeforces 781E Andryusha and Nervous Barriers 线段树 单调栈

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF781E.html 题目传送门 - CF781E 题意 有一个矩形,宽为 w ,高为 h .一开始会有 w 个 ...

  9. node 简单的爬虫

    基于express爬虫, 1,node做爬虫的优势 首先说一下node做爬虫的优势 第一个就是他的驱动语言是JavaScript.JavaScript在nodejs诞生之前是运行在浏览器上的脚本语言, ...

  10. gitalk报错问题

    今天为了优化个人博客,将博客同时部署到Github和Coding,之后虽然博客访问速度相比以前有很大的提升,但是不知道gitalk评论会莫名出现问题 问题 点击使用GitHub登陆会跳转到个人博客主页 ...