/**
题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图
思路:
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 解题报告的更多相关文章

  1. PAT-Top1001. Battle Over Cities - Hard Version (35)

    在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...

  2. 【LeetCode】278. First Bad Version 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...

  3. Lintcode: First Bad Version 解题报告

    First Bad Version http://lintcode.com/en/problem/first-bad-version The code base version is an integ ...

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

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

  5. codeforces B. Ping-Pong (Easy Version) 解题报告

    题目链接:http://codeforces.com/problemset/problem/320/B 题目意思:有两种操作:"1 x y"  (x < y) 和 " ...

  6. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  7. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告

    Facebook Hacker Cup 2014 Qualification Round比赛Square Detector题的解题报告.单击这里打开题目链接(国内访问需要那个,你懂的). 原题如下: ...

  9. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

随机推荐

  1. JS计算混合字符串长度

    用的是正则表达式 var str = ”坦克是tank的音译”; var len = str.match(/[^ -~]/g) == null ? str.length : str.length +  ...

  2. flask-login 整合 pyjwt + json 简易flask框架

    现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...

  3. Beyond Compare文本对比中提示编辑禁止的解决方法

    Beyond Compare是一款拥有文本比较功能的智能化软件,它支持在文本比较的同时,直接对差异文本进行修改.删除.编辑等一系列操作,这样一来,节约了文本对比的时间.但是在使用Beyond Comp ...

  4. python + selenium webdriver 自动化测试 之 环境异常处理 (持续更新)

    1.webdriver版本与浏览器版本不匹配,在执行的时候会抛出如下错误提示 selenium.common.exceptions.WebDriverException: Message: unkno ...

  5. Teaching Machines to Understand Us 让机器理解我们 之二 深度学习的历史

    Deep history 深度学习的历史 The roots of deep learning reach back further than LeCun’s time at Bell Labs. H ...

  6. B1030 完美数列 (25 分)

    这是一道二分法的题目,许久不使用二分法,感觉有点生疏. #include<bits/stdc++.h> using namespace std; const int MAXN=100000 ...

  7. PAT甲题题解-1025. PAT Ranking (25)-排序

    排序,求整体的排名和局部的排名整体排序,for循环一遍同时存储整体目前的排名和所在局部的排名即可 #include <iostream> #include <cstdio> # ...

  8. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

  9. Final版本互评——杨老师粉丝群《PinBall》

    基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 随着民族自信的觉醒,民主文化越来越受到重视,语文在高考中的比重也不断增加,在这种大环境下,成 ...

  10. 关于如何在Tomcat中使用JavaBean

    对于没有使用myeclipse,NetBean等IDE工具的用户,如果在编写JSP时,用到了java文件,就必须配置JAVAbean了,网上也有很多在Tomcat中配置JAVABean的例子,这里我简 ...