pat1001. Battle Over Cities - Hard Version 解题报告
/**
题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图
思路:
prim+最小堆
1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点
2.求出不同点集合的最短距离,用prim+最小堆求出最小生成树
kruskal
1.之前图中未破坏的边必用,全部加到图中
2.途中被破坏的边按照边权从小到大的顺序依次加入图中,直到图变为连通图
两个方法的对应一个点的最小生成树的复杂度都是nlogm,第二个方法较好写
优化:
1.未破坏的边直接加入图中
2.当有n-2条边(当前删去一个点后,图中n-1个点)在图中时,直接结束建生成树
另外:
求图的连通性:
有向 强连通
无向 并查集
当然,并查集好写很多。。。
**/
#include <stdio.h>
#include <stdlib.h>
#define maxn 500
#define maxm 125000
#define maxdist 100000 /**
题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图
思路:
prim+最小堆
1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点
2.求出不同点集合的最短距离,用prim+最小堆求出最小生成树 kruskal
1.之前图中未破坏的边必用,全部加到图中
2.途中被破坏的边按照边权从小到大的顺序依次加入图中,直到图变为连通图 两个方法的对应一个点的最小生成树的复杂度都是nlogm,第二个方法较好写 优化:
1.未破坏的边直接加入图中
2.当有n-2条边(当前删去一个点后,图中n-1个点)在图中时,直接结束建生成树 另外:
求图的连通性:
有向 强连通
无向 并查集 当然,并查集好写很多。。。
**/ struct node
{
long x,y,c,s;
}edge0[maxm+],edge1[maxm+];
long fa[maxn+]; long max(long a,long b)
{
if (a>b)
return a;
else
return b;
} int cmp(const void *a,const void *b)
{
//先把未损坏的路径加入图中
if ((*(struct node *)a).s < (*(struct node *)b).s)
return ;
else if ((*(struct node *)a).s > (*(struct node *)b).s)
return -;
//再对已损坏的路径进行排序,用kruskal算法,m log m
else if ((*(struct node *)a).c < (*(struct node *)b).c)
return ;
else
return -;
} long getfather(long d)
{
if (fa[d]==d)
return d;
else
return getfather(fa[d]);
} int main()
{
long i,j,n,m,p,q,x,y,c,s,g,fx,fy,ans,cost[maxn+];
while (scanf("%ld%ld",&n,&m)!=EOF)
{
p=;
q=;
for (i=;i<m;i++)
{
scanf("%ld%ld%ld%ld",&x,&y,&c,&s);
if (s==)
{
p++;
edge1[p].x=x; edge1[p].y=y; edge1[p].c=c; edge1[p].s=s;
}
else
{
q++;
edge0[q].x=x; edge0[q].y=y; edge0[q].c=c; edge0[q].s=s;
}
}
qsort(edge0+,q,sizeof(struct node),cmp); ans=;
for (i=;i<=n;i++)
{
for (j=;j<=n;j++)
fa[j]=j;
g=;
//edge - exist
for (j=;j<=p;j++)
{
if (edge1[j].x==i || edge1[j].y==i) continue;
fx=getfather(edge1[j].x);
fy=getfather(edge1[j].y);
if (fx==fy) continue;
fa[fx]=fy;
g++;
if (g==n-)
break;
}
//优化
if (g==n-)
{
cost[i]=;
continue;
} //edge - not exist
cost[i]=;
for (j=;j<=q;j++)
{
if (edge0[j].x==i || edge0[j].y==i) continue;
fx=getfather(edge0[j].x);
fy=getfather(edge0[j].y);
if (fx==fy) continue;
fa[fx]=fy;
g++;
cost[i]+=edge0[j].c;
//优化
if (g==n-)
break;
}
if (g<n-)
cost[i]=maxdist;
ans=max(ans,cost[i]);
}
if (ans>)
{
for (i=;i<=n;i++)
if (cost[i]==ans)
{
printf("%ld",i);
break;
}
for (j=i+;j<=n;j++)
if (cost[j]==ans)
printf(" %ld",j);
printf("\n");
}
else
printf("0\n");
}
return ;
}

pat1001. Battle Over Cities - Hard Version 解题报告的更多相关文章
- PAT-Top1001. Battle Over Cities - Hard Version (35)
在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...
- 【LeetCode】278. First Bad Version 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...
- Lintcode: First Bad Version 解题报告
First Bad Version http://lintcode.com/en/problem/first-bad-version The code base version is an integ ...
- 「日常训练」Battle Over Cities - Hard Version(PAT-TOP-1001)
题意与分析 题意真的很简单,实在不想讲了,简单说下做法吧. 枚举删除每个点,然后求最小生成树,如果这个路已经存在那么边权就是0,否则按照原来的处理,之后求花费,然后判整个图是否联通(并查集有几个roo ...
- codeforces B. Ping-Pong (Easy Version) 解题报告
题目链接:http://codeforces.com/problemset/problem/320/B 题目意思:有两种操作:"1 x y" (x < y) 和 " ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告
Facebook Hacker Cup 2014 Qualification Round比赛Square Detector题的解题报告.单击这里打开题目链接(国内访问需要那个,你懂的). 原题如下: ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
随机推荐
- LCA的一些算法
LCA,就是求树上任意两点的最近公共祖先 (本题图片与代码均为Luogu3379) 方法我好像讲过一个,这次把主要的三个一起讲一讲 <1> 倍增(O(n log n)) 我们先考虑最基本的 ...
- mfc CSpinButton
知识点: CSliderCtrl(滑块)控件 CSliderCtrl常用属性 CSliderCtrl类常用成员函数 CSliderCtrl运用示例 一.CSliderCtr常用属性 Orientati ...
- Hadoop日记Day7---HDFS的WED端口
HDFS 对外提供了可供访问的http server,开放了很多端口,下面介绍常用的几个端口(http://hadoop:……). 一.50070 端口,查看NameNode 状态,如图1.1所示. ...
- 5.Xilinx RapidIO核例子工程源码分析
https://www.cnblogs.com/liujinggang/p/10091216.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:V ...
- 蓝牙Remove Bond的流程分析
此篇文章简单分析一下蓝牙解除配对在协议栈中的工作流程.分析的协议栈版本是Android8.0 协议栈的接口都定义在bluetooth.cc这个文件中: static int remove_bond(c ...
- Spring+SpringMVC+MyBatis整合基础篇(三)搭建步骤
作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载. 框架介绍 Spring SpringMVC MyBatis easyUI ...
- NetBeans 插件开发简介
希望 NetBeans 为您提供更多功能吗? 您希望倾心投入到 NetBeans 的开发中,并希望它能激发您开发另一个应用程序的热情.您希望聆听音乐.浏览网页.查看邮件.存储喜欢的 URL,以及维护日 ...
- Java设计模式之适配器设计模式(项目升级案例)
今天是我学习到Java设计模式中的第三个设计模式了,但是天气又开始变得狂热起来,对于我这个凉爽惯了的青藏人来说,又是非常闹心的一件事儿,好了不管怎么样,目标还是目标(争取把23种Java设计模式接触一 ...
- win10安装tensorflow-gpu
1.安装anaconda (最好使用清华源下载) 2.打开cmd conda create -n tensorflow pip python=3.6 activate tensorflow pip i ...
- gym101522 [小熊骑士限定]La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017
西瓜队(划掉),Kuma Rider久违的第一场训练,四小时瞎打.jpg A.水题,排序 #include<cstdio> #include<iostream> #includ ...