poj_1679 The Unique MST
来源:互联网 发布:数据缺失的处理方法 编辑:程序博客网 时间:2024/04/29 13:44
The Unique MST
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 14582
Accepted: 5035
题目连接:http://poj.org/problem?id=1679
Description
Given a connected undirectedgraph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V,E). A spanning tree of G is a subgraph of G, say T = (V', E'), with thefollowing properties:
1. V' = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected,undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is thespanning tree that has the smallest total cost. The total cost of T means thesum of the weights on all the edges in E'.
Input
The first line contains asingle integer t (1 <= t <= 20), the number of test cases. Each caserepresents a graph. It begins with a line containing two integers n and m (1<= n <= 100), the number of nodes and edges. Each of the following m linescontains a triple (xi, yi, wi), indicating that xi and yi are connected by anedge with weight = wi. For any two nodes, there is at most one edge connectingthem.
Output
For each input, if the MST isunique, print the total cost of it, or otherwise print the string 'NotUnique!'.
Sample Input
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
Sample Output
3
Not Unique!
Source
POJMonthly--2004.06.27 srbga@POJ
题意:
判断最小生成树是否唯一,即是否有两个一模一样的最小生成树的值
解题思路:
这是一个求次小生成树的题目,可以依照求最小生成树的方法来求,但是求最小生成树的时候要保存该生成树的每一条边,在删除某一条边后在求最小生成树,比较是否相等
代码:
//求次小生成树
/*
首先求出原图最小生成树,记录权值之和为MinST。枚举添加每条不在最小生成树上的边(u,v),
加上以后一定会形成一个环。找到环上权值第二大的边(即除了(u,v)以外的权值最大的边),把
它删掉,计算当前生成树的权值之和。取所有枚举修改的生成树权值之和的最小值,就是次小生
成树。
*/
#include <iostream>#include<cstdio>#include<cstring>#define MAX 200#define VALUE 0xfffffffusing namespace std;//存储最小生成树的边struct des{int w;int s;int e;};int g[MAX][MAX];int visited[MAX];des minCost[MAX];int v,e;//求最小生成树int prim(){ int i; for(i=0;i<=v;i++){ minCost[i].w=VALUE;minCost[i].s=0;minCost[i].e=0;visited[i]=0;} minCost[1].w=0; int res=0; while(true) { int t=-1;int a=0; for(i=1;i<=v;i++) { if(visited[i]==0 && (t==-1 || minCost[i].w<minCost[t].w)){ t=i;minCost[t].e=minCost[i].e;minCost[t].s=minCost[i].s;minCost[t].w=minCost[i].w;} } //每条边都遍历完 if(t==-1) break; //访问t点 visited[t]=1;if(minCost[t].w==VALUE)return -1;//不连通 res+=minCost[t].w; //printf("%d\t",minCost[t].w); for(i=1;i<=v;i++) { if(minCost[i].w>g[t][i] && g[t][i]!=0) { minCost[i].w=g[t][i];minCost[i].s=t;minCost[i].e=i; } } } return res;}void secondShortPath(){ int path1=prim();//最短路径if(path1==-1 || path1==0){printf("0\n");return ;} int i;//枚举最小生成树的每一条边,des tempCost[MAX];//保存原来的值for(i=0;i<=v;i++){tempCost[i]=minCost[i];}for(i=1;i<=v;i++){if(tempCost[i].s!=0 && tempCost[i].e!=0){//假设最小生成树中某一条边没了,再求最小生成树,看是否还相等g[tempCost[i].s][tempCost[i].e]=0;//minCost值发生了改变g[tempCost[i].e][tempCost[i].s]=0;int path2=prim();if(path2==-1)//不连通continue;if(path1==path2)//最小生成树不唯一{printf("Not Unique!\n");return ;}//还原之前删除的边g[tempCost[i].s][tempCost[i].e]=tempCost[i].w;g[tempCost[i].e][tempCost[i].s]=tempCost[i].w;}} printf("%d\n",path1);}int main(){int ts;scanf("%d",&ts);while(ts--){memset(g,0,sizeof(g));scanf("%d%d",&v,&e);int i;int start,end,weight;for(i=0;i<e;i++){scanf("%d%d%d",&start,&end,&weight);g[start][end]=weight;g[end][start]=weight;}secondShortPath();} return 0;}
- poj_1679 The Unique MST
- poj_1679 The Unique MST(次小生成树)
- The Unique MST
- pku1679 The Unique MST
- PKU1679 the unique mst
- POJ1679:The Unique MST
- The Unique MST
- The Unique MST(P1679)
- POJ1679 The Unique MST
- 【hdu】 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj The Unique MST
- poj1679 The Unique MST
- The Unique MST
- poj1679 The Unique MST
- The Unique MST
- The Unique MST--hdoj
- MFC控件扩展性设置(按钮、图片、超链接)
- 从HTML5统治世界的说法来看Native APP 和 WEB APP 的差别
- openstack下虚拟机的监控
- S3C2440 cp15协处理器详解
- cman not started: Local host name resolves to 127.0.0.1; fix /etc/hosts before starting cluster
- poj_1679 The Unique MST
- oracle查看用户所在表空间
- RTX Server SDK跨服务器调用 发消息提醒
- hdu 1106 排序
- Android的TextView使用Html来处理图片显示、字体样式、超链接等
- hdu 1174 爆头
- OpenStack Nova启动一个虚拟机内部流程
- What is my plan?
- migo收货后查不到凭证