UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)...
Input:
Input starts with an integer t ≤ 1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v (1 ≤ v ≤ 100) and e (0 ≤ e ≤ 200). v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form ‘start end cost’, where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.
Output:
There may be three cases in the output
1. No way to complete the task,
2. There is only one way to complete the task,
3. There are more than one way.
Output ‘No way’ for the first case, ‘No second way’ for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.
Sample Input:
4
5 4
1 2 5
3 2 5
4 2 5
5 4 5
5 3
1 2 5
3 2 5
5 4 5
5 5
1 2 5
3 2 5
4 2 5
5 4 5
4 5 6
1 0
Sample Output:
Case #1 : No second way
Case #2 : No way
Case #3 : 21
Case #4 : No second way
题目大意:给你n个顶点,m条边。如果图是不连通的,输出No way,如果没有次小生成树,输出No second way,如果有次小生成树,输出次小生成树的值。有重边。
解题思路:对于有重边的情况,我们可以用kruskal做,枚举删除最小生成树上的边,进行n-1次枚举,更新出次小生成树。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
struct Edge{
int from,to,dist;
}edges[maxn*maxn];
struct Set{
int pa,rela;
}sets[maxn];
int store[maxn];
bool cmp(Edge a,Edge b){
return a.dist < b.dist;
}
void init(int n){
for(int i = 0; i <= n; i++){
sets[i].pa = i;
}
}
int Find(int x){
if(x == sets[x].pa){
return x;
}
int tmp = sets[x].pa;
sets[x].pa = Find(tmp);
return sets[x].pa;
}
int num =0;
int Kruskal(int n,int m){
init(n);
int rootx,rooty,x,y;
int retsum = 0;
num = 0;
for(int i = 0; i < m; i++){
Edge & e = edges[i];
x = e.from, y = e.to;
rootx = Find(x);
rooty = Find(y);
if(rootx != rooty){
store[num++] = i;
retsum += edges[i].dist;
sets[rooty].pa = rootx;
}
}
if(num < n-1){
return -1;
}else{
return retsum;
}
}
int SecKruskal(int n,int m,int mark){
init(n);
int rootx,rooty,x,y;
int retsum = 0, cnt = 0;
for(int i = 0; i < m; i++){
if(mark == i) continue;
Edge & e = edges[i];
x = e.from, y = e.to;
rootx = Find(x);
rooty = Find(y);
if(rootx != rooty){
cnt++;
retsum += edges[i].dist;
sets[rooty].pa = rootx;
}
}
if(cnt < n-1){
return INF;
}else{
return retsum;
}
}
int main(){
int T,n,m,cas = 0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int a,b,c;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
a--,b--;
edges[i].from = a;
edges[i].to = b;
edges[i].dist = c;
}
sort(edges,edges+m,cmp);
int mst = Kruskal(n,m);
printf("Case #%d : ",++cas);
if(mst == -1){
puts("No way");
continue;
}
int ans = INF;
for(int i = 0; i < num; i++){
int tmp = SecKruskal(n,m,store[i]);
ans = min(ans,tmp);
}
if(ans == INF){
puts("No second way");
}else{
printf("%d\n",ans);
}
}
return 0;
}
UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】的更多相关文章
- UVA 10462 Is There A Second Way Left? 次小生成树
模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...
- UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解
思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...
- UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...
- UVA - 10462 Is There A Second Way Left?
题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...
- 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- UVA - 1279 Asteroid Rangers (动点的最小生成树)
题意,有n个匀速动点,求最小生成树的改变次数. 一句话总结:动态问题的一般做法是先求出一个静态的解,然后求出解发生改变的事件,事件按照时间排序,依次处理. 先求出最开始的最小生成树(MST),当MST ...
- UVA - 1395 Slim Span (最小生成树Kruskal)
Kruskal+并查集. 点很少,按边权值排序,枚举枚举L和R,并查集检查连通性.一旦连通,那么更新答案. 判断连通可以O(1),之前O(n)判的,第一次写的过了,后来T.. #include< ...
- 【uva 1151】Buy or Build(图论--最小生成树+二进制枚举状态)
题意:平面上有N个点(1≤N≤1000),若要新建边,费用是2点的欧几里德距离的平方.另外还有Q个套餐,每个套餐里的点互相联通,总费用为Ci.问让所有N个点连通的最小费用.(2组数据的输出之间要求有换 ...
随机推荐
- c++位运算符 | & ^ ~ && ||,补码,反码
一:简介 1 位逻辑运算符: & (位 “与”) and ^ (位 “异或”) | (位 “或”) or ~ (位 “取反” ...
- Velodyne VPL16 configuration in ROS Kinetic
1. 驱动安装 sudo apt-get install ros-kinetic-velodyne 2. 在已有工作空间catkin_ws中,添加Velodyne包 cd ~/catkin_ws/sr ...
- java基础之转义符、数据类型
一. 转义符 1.\n \n的作用是换行,也就是和键盘上的回车键相同 2.\t \t的作用是制表,就是以八个空格为一个单位,当不足八个时会自动补齐八个,如asd\tfgh,那么输出的将会是 . 3. ...
- TeamLeader管理方法
1. 规划 在加强质量的同时,提升团队业务理解能力推动产品经理深入度增加业务监控 2. 洗脑 现在离开去bat,前两年会学习,但可能无人带领待3-5年,做到B类从基金学习起,学习金融学习架构设计提升团 ...
- Python Flask模块
模块是一个包含响应文本的文件,其中包含占用位变量表示的动态部分,其具体值只在请求的上下文中才知道.使用真实值替换变量,再返回最终得到的响应字符串,这一过程称为渲染.为了渲染模块,Flask使用一个名为 ...
- v$sqlarea,v$sql,v$sqltext这三个视图提供的sql语句有什么区别?
v$sqltext存储的是完整的SQL,SQL被分割 SQL> desc v$sqltextName Null? ...
- Java foreach remove问题分析
原文链接:http://www.cnblogs.com/chrischennx/p/9610853.html 都说ArrayList在用foreach循环的时候,不能add元素,也不能remove元素 ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 图像标注工具labelImg安装记录
这里仅记载下labelImg的安装过程,因为有坑. 我的安装方式是从源码编译,环境ubuntu16.04,一开始是使用python2安装,从github上下载好源码,然后执行安装命令 sudo apt ...
- Qt中的布局管理器
1. 布局管理器提供相关的类对界面组件进行布局管理,能够自动排列窗口中的界面组件,窗口变化后能自动更新界面组件的大小. 2. QLayout是Qt布局管理器的抽象基类,通过继承QLayout实现了功能 ...