您的位置:首页 > 其它

UVa11183 - Teen Girl Squad(最小树形图-裸)

2017-06-29 14:01 691 查看
ProblemI

TeenGirlSquad

Input:
StandardInput

Output:StandardOutput

--3springrollsplease.

--MSG'D!!

--Oh!Mystomachlining!

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.

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


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
usingnamespacestd;
constintmaxn=1000+10;
constintinf=1<<25;
structedge{
intu,v,w;
edge(intu,intv,intw):u(u),v(v),w(w){}
};
vector<edge>e;
intn,m;
intpre[maxn],inv[maxn],to[maxn],ID[maxn];

intZhuLiu(intrt){
intret=0;
while(true){
for(inti=0;i<n;i++){
inv[i]=inf;
ID[i]=-1;
to[i]=-1;
}
for(inti=0;i<m;i++){
intu=e[i].u,v=e[i].v,w=e[i].w;
if(inv[v]>w&&u!=v){
inv[v]=w;
pre[v]=u;
}
}
inv[rt]=0;
for(inti=0;i<n;i++){
if(inv[i]==inf)return-1;
}
intcnt=0;
for(inti=0;i<n;i++){
ret+=inv[i];
intv=i;
while(to[v]!=i&&ID[v]==-1&&v!=rt){
to[v]=i;
v=pre[v];
}
if(ID[v]==-1&&v!=rt){
for(intu=pre[v];u!=v;u=pre[u])ID[u]=cnt;
ID[v]=cnt++;
}
}
if(cnt==0)break;
for(inti=0;i<n;i++){
if(ID[i]==-1)ID[i]=cnt++;
}
for(inti=0;i<m;i++){
intu=e[i].u,v=e[i].v,w=e[i].w;
e[i].w-=inv[v];
e[i].u=ID[u];
e[i].v=ID[v];
}
rt=ID[rt];
n=cnt;

}
returnret;

}
intmain(){
intncase,T=1;
cin>>ncase;
while(ncase--){
e.clear();
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++){
intu,v,w;
scanf("%d%d%d",&u,&v,&w);
e.push_back(edge(u,v,w));
}
intans=ZhuLiu(0);
if(ans==-1){
printf("Case#%d:Possums!\n",T++);
}else{
printf("Case#%d:%d\n",T++,ans);
}

}
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: