uva11183 Teen Girl Squad(最小树形图)
2015-01-28 20:05
375 查看
TeenGirlSquad
Input:StandardInput
Output:StandardOutput
StrongBad
Youarepartofagroupofnteenagegirlsarmedwithcellphones.Youhavesomenewsyouwanttotelleveryoneinthegroup.Theproblemisthatnotwoofyouareinthesameroom,andyoumustcommunicateusingonlycellphones.What'sworse
isthatduetoexcessiveusage,yourparentshaverefusedtopayyourcellphonebills,soyoumustdistributethenewsbycallingeachotherinthecheapestpossibleway.Youwillcallseveralofyourfriends,theywillcallsomeoftheirfriends,andsoon
untileveryoneinthegrouphearsthenews.
Eachofyouisusingadifferentphoneserviceprovider,andyouknowthepriceofgirlAcallinggirlBforallpossibleAandB.Notallofyourfriendslikeeachother,andsomeofthemwillnevercallpeopletheydon'tlike.Yourjobistofindthecheapest
possiblesequenceofcallssothatthenewsspreadsfromyoutoalln-1othermembersofthegroup.
Input
Thefirstlineofinputgivesthenumberofcases,N(N<150).Ntestcasesfollow.Eachonestartswithtwolinescontainingn(0<=n<=1000)andm(0<=m<=
40,000).Girlsarenumberedfrom0ton-1,andyouaregirl0.Thenextmlineswilleachcontain3integers,u,vandw,meaningthatacallfromgirluto
girlvcostswcents(0<=w<=1000).Noothercallsarepossiblebecauseofgrudges,rivalriesandbecausetheyare,like,lame.Theinputfilesizeisaround1200KB.
Input:StandardInput
Output:StandardOutput
--3springrollsplease. --MSG'D!! --Oh!Mystomachlining! |
Youarepartofagroupofnteenagegirlsarmedwithcellphones.Youhavesomenewsyouwanttotelleveryoneinthegroup.Theproblemisthatnotwoofyouareinthesameroom,andyoumustcommunicateusingonlycellphones.What'sworse
isthatduetoexcessiveusage,yourparentshaverefusedtopayyourcellphonebills,soyoumustdistributethenewsbycallingeachotherinthecheapestpossibleway.Youwillcallseveralofyourfriends,theywillcallsomeoftheirfriends,andsoon
untileveryoneinthegrouphearsthenews.
Eachofyouisusingadifferentphoneserviceprovider,andyouknowthepriceofgirlAcallinggirlBforallpossibleAandB.Notallofyourfriendslikeeachother,andsomeofthemwillnevercallpeopletheydon'tlike.Yourjobistofindthecheapest
possiblesequenceofcallssothatthenewsspreadsfromyoutoalln-1othermembersofthegroup.
Input
Thefirstlineofinputgivesthenumberofcases,N(N<150).Ntestcasesfollow.Eachonestartswithtwolinescontainingn(0<=n<=1000)andm(0<=m<=
40,000).Girlsarenumberedfrom0ton-1,andyouaregirl0.Thenextmlineswilleachcontain3integers,u,vandw,meaningthatacallfromgirluto
girlvcostswcents(0<=w<=1000).Noothercallsarepossiblebecauseofgrudges,rivalriesandbecausetheyare,like,lame.Theinputfilesizeisaround1200KB.
Output
Foreachtestcase,outputonelinecontaining"Case#x:"followedbythecostofthecheapestmethodofdistributingthenews.Ifthereisnosolution,print"Possums!"instead.SampleInputOutputforSampleInput
4 2 1 0110 2 1 1010 4 4 0110 0210 1320 2330 4 4 0110 1220 2030 23100 | Case#1:10 Case#2:Possums! Case#3:40 Case#4:130 |
/*
最小树形图模板题。。
Time:2015-1-2820:03
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
usingnamespacestd;
typedeflonglongLL;
constintINF=0x3f3f3f3f;
constintMAX=1005;
structEdge{
intu,v;
intw;
}edge[40004];
intvis[MAX],id[MAX],pre[MAX],minIn[MAX];
intDirect_Mst(intrt,intV,intE){
LLret=0;
intu,v;
while(1){
for(inti=0;i<=V;i++)
minIn[i]=INF;
for(inti=0;i<E;i++){
u=edge[i].u;v=edge[i].v;
if(minIn[v]>edge[i].w&&u!=v){
minIn[v]=edge[i].w;
pre[v]=u;
}
}
for(inti=0;i<V;i++)if(i!=rt&&minIn[i]==INF)return-1;
memset(id,-1,sizeof(id));
memset(vis,-1,sizeof(vis));
intcnt=0;minIn[rt]=0;
for(inti=0;i<V;i++){
ret+=(LL)minIn[i];
v=i;
while(vis[v]!=i&&id[i]==-1&&v!=rt){
vis[v]=i;v=pre[v];
}
if(id[v]==-1&&v!=rt){
for(u=pre[v];u!=v;u=pre[u]){
id[u]=cnt;
}
id[v]=cnt++;
}
}
if(cnt==0)break;//只剩下根,不再循环
for(inti=0;i<V;i++)if(id[i]==-1)id[i]=cnt++;//都编号
for(inti=0;i<E;i++){
u=edge[i].u;v=edge[i].v;
edge[i].u=id[u];edge[i].v=id[v];
if(id[u]!=id[v]){
edge[i].w-=minIn[v];
}
}
V=cnt;rt=id[rt];
}
returnret;
}
intmain(){
intT,n,m;
intnCase=1;
intu,v,w;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
edge[i].u=u;edge[i].v=v;
if(u==v)edge[i].w=INF;//去自环
else{
edge[i].w=w;
}
}
LLans=Direct_Mst(0,n,m);
printf("Case#%d:",nCase++);
if(ans==-1)
printf("Possums!\n");
else
printf("%lld\n",ans);
}
return0;
}
相关文章推荐
- UVa11183 - Teen Girl Squad(最小树形图-裸)
- UVA 11183 Teen Girl Squad(最小树形图)
- Uva 11183 Teen Girl Squad(最小树形图 模板)
- UVa11183 - Teen Girl Squad(最小树形图-裸)
- uva11183 Teen Girl Squad(最小树形图)
- uva11183 Teen Girl Squad(最小树形图朱刘算法)
- UVA - 11183 Teen Girl Squad(最小树形图)
- UVA 11183 - Teen Girl Squad(最小树形图)
- UVA-11183- Teen Girl Squad(最小树形图模板)
- UVA 11183 Teen Girl Squad(最小树形图裸题)
- UVA11183 Teen Girl Squad (最小树形图)
- UVA 11183 Teen Girl Squad(最小树形图)
- kuangbin专题八 UVA11183 Teen Girl Squad(最小树形图)
- uva 11183 Teen Girl Squad (最小树形图)
- UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)
- UVA 11183 Teen Girl Squard(最小树形图)
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
- UVA 11183 Teen Girl Squad (最小树形图)
- Teen Girl Squad - UVA 11183 - 最小树形图
- UVA 11183 Teen Girl Squad 最小树形图裸题