hdu3371
来源:互联网 发布:p2p java源代码 编辑:程序博客网 时间:2024/05/07 22:32
/*
分析:
简单prim,还connected的边将其长度记为0就行了。
在k次读入的时候,每次给了t个数,不用进行C(t,2)次赋值让map[a][b]=map[b][a]=0,
只用进行t-1次就行了,让这t个数成一个链啊什么的,不用出现回路。
2012-06-17
*/
分析:
简单prim,还connected的边将其长度记为0就行了。
在k次读入的时候,每次给了t个数,不用进行C(t,2)次赋值让map[a][b]=map[b][a]=0,
只用进行t-1次就行了,让这t个数成一个链啊什么的,不用出现回路。
2012-06-17
*/
#include"stdio.h"#include"stdlib.h"struct A{int a,b;int len;}E[25011];int cmp(const struct A *a,const struct A *b){return a->len-b->len;}int main(){int T;int n,m,k;int i,l;int tail;int ans;int flag;int map[501][501];int hash[501];int a,b,len;int t,temp[501],i2;scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&k);for(i=1;i<=n;i++)for(l=1;l<=n;l++){if(i==l)map[i][l]=map[l][i]=0;elsemap[i][l]=map[l][i]=11111111;}for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&len);map[a][b]=map[b][a]=map[a][b]>len?len:map[a][b];}for(i=0;i<k;i++){scanf("%d",&t);for(l=1;l<=t;l++)scanf("%d",&temp[l]);for(i2=2;i2<=t;i2++)map[temp[i2]][temp[i2-1]]=map[temp[i2-1]][temp[i2]]=0;}tail=0;for(i=1;i<n;i++)for(l=i+1;l<=n;l++){if(map[i][l]!=11111111){E[tail].a=i;E[tail].b=l;E[tail].len=map[i][l];tail++;}}qsort(E,tail,sizeof(E[0]),cmp);for(i=1;i<=n;i++)hash[i]=1;ans=0;hash[E[0].a]=0;for(i=0;i<tail;i++){flag=hash[E[i].a]+hash[E[i].b];if(flag==1){ans+=E[i].len;hash[E[i].a]=hash[E[i].b]=0;i=0;}}flag=0;for(i=1;i<=n;i++)if(hash[i]){flag=1;break;}if(flag)printf("-1\n");elseprintf("%d\n",ans);}return 0;}
- hdu3371
- hdu3371
- HDU3371
- HDU3371
- hdu3371
- HDU3371&&POJ2485
- hdu3371 Connect the Cities
- hdu3371一个“ ; ”一个郁闷
- HDU3371--Connect the Cities
- hdu3371 Connect the Cities
- HDU3371 Connect the Cities
- hdu3371: Connect the Cities
- Hdu3371【最小生成树】
- HDU3371--- Connect the Cities
- HDU3371-Connect the Cities
- hdu3371 Connect the Cities
- hdu3371之最小生成树
- HDU3371 Connect the Cities【Kruskal】
- Oracle 外部表创建
- 给从事软硬件开发行业的同学们的一些建议
- ext.net 中的 Layout
- EOF是什么?
- ARM11协处理器简介
- hdu3371
- 天猫抢红包小工具源码
- MyEclipse集成svn Eclipse集成svn
- 在CentOS上安装Git
- loadrunner、QTP、TestDirector/Quality Center、Rational Robot学习笔记
- 远程文件管理工具Win RFC
- 事件的处理
- Linux常用命令
- VC++中可编辑的CListCtrl实现