URAl 1416Confidential【次小生成树】
来源:互联网 发布:深圳华强北现状知乎 编辑:程序博客网 时间:2024/05/18 02:04
1416. Confidential
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.
Input
The first line contains integers n and m that are a number of planets in the Galaxy and an amount of passages between them (2 ≤ n ≤ 500). The next m lines contain integers ai, bi and wi that are the numbers of the planets connected with the passage and the transition cost (1 ≤ ai, bi ≤ n; 0 ≤ wi≤ 1000). If an A to B transition is possible then a B to A transition is possible too, and the cost of these transitions are equal. There is no more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.
Output
You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by −1.
Samples
4 61 2 22 3 23 4 24 1 21 3 12 4 1
Cost: 4Cost: 4
3 21 2 22 3 2
Cost: 4Cost: -1
Problem Author: Den Raskovalov
Problem Source: The Ural State University Championship, October 29, 2005
Problem Source: The Ural State University Championship, October 29, 2005
题意:
n个顶点,m 条边,给出每条边连接的顶点编号,以及边的权值,求使得所有点相连通需要的边的最小的权值和以及第二小的权值和是多少,不存在的话,输出-1
题解:
求最小生成树和次小生成树(非严格次小,也就是权值可以和最小的相等)是否存在,存在的话,输出他们的权值
模板,点这里,稍微修改下模板,就能AC了....
/*http://blog.csdn.net/liuke19950717*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int inf=0x3f3f3f3f;const int maxn=505;int map[maxn][maxn];bool vis[maxn];int low[maxn];int mst[maxn][maxn],pre[maxn];bool inmst[maxn][maxn];int n,m;void init(){memset(map,inf,sizeof(map));for(int i=1;i<=n;++i){map[i][i]=0;}}int mintree(){int mincost=0;int next,Min;memset(inmst,0,sizeof(inmst));memset(mst,0,sizeof(mst));for(int i=1;i<=n;++i){low[i]=map[1][i];vis[i]=0;pre[i]=1;}vis[1]=1;for(int i=2;i<=n;++i){Min=inf;next=-1;for(int j=1;j<=n;++j){if(!vis[j]&&Min>low[j]){next=j;Min=low[j];}}mincost+=Min;if(next==-1){return -1;}vis[next]=1;int fa=pre[next];inmst[next][fa]=inmst[fa][next]=1;for(int j=1;j<=n;++j){if(vis[j]&&j!=next){mst[j][next]=mst[next][j]=max(mst[fa][j],low[next]);}if(!vis[j]&&low[j]>map[next][j]){low[j]=map[next][j];pre[j]=next;}}}return mincost;}int sectree(int x)//参数是最小生成树的权值 {int ans=inf;for(int i=1;i<=n;++i){for(int j=1;j<i;++j){if(map[i][j]!=inf&&!inmst[i][j]){ans=min(ans,x-mst[i][j]+map[i][j]);}}}return ans==inf?-1:ans;//不存在的为-1,否则返回权值 }int main(){while(~scanf("%d%d",&n,&m)){init();for(int i=1;i<=m;++i){int a,b,c;scanf("%d%d%d",&a,&b,&c);map[a][b]=map[b][a]=min(map[a][b],c);}int cost1=mintree(),cost2=cost1==-1?-1:sectree(cost1);printf("Cost: %d\n",cost1);printf("Cost: %d\n",cost2);}return 0;}
0 0
- Ural 1416 Confidential,次小生成树
- URAL - 1416 Confidential(次小生成树)
- URAl 1416Confidential【次小生成树】
- URAL 1416 Confidential --最小生成树与次小生成树
- Ural 1416 Confidential(最小生成树+次小生成树)
- poj1679 The Unique MST(次小生成树) Ural 1416 Confidential
- Confidential----Ural_1416----次小生成树
- URAL 1416 Confidential <最小生成数和次小生成数>
- ural 1416 Confidential
- URAL 1416 Confidential
- URAL 1416 Confidential
- URAL-1416 (典型的次小生成树)
- ural 1416. Confidential
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- 次小生成树
- Linux 内核模块简介及常用命令
- POJ 3261 后缀数组
- LeetCode 227 Basic Calculator II
- linux sort命令
- myapps 会议管理设计与实现
- URAl 1416Confidential【次小生成树】
- iOS 视图抖动动画
- More Effective C++----(27)要求或禁止在堆中产生对象
- Linux块设备驱动: ramdisk
- jquery 扩展方法 hello world
- [leetcode] 93. Restore IP Addresses 解题报告
- dedecms调用文章循环次数并判断
- Linux/Unix inode、vnode、dentry、file、进程表、文件表(中)
- iOS 集成环信(四)