您的位置:首页 > 其它

UVA 11183 Teen Girl Squad(最小树形图)

2015-02-15 13:50 429 查看
TeenGirlSquad

Input:StandardInput

Output:StandardOutput

--3springrollsplease.

--MSG'D!!

--Oh!Mystomachlining!

Strong
Bad

Youarepartofagroupofnteenagegirlsarmedwith
cellphones.Youhavesomenewsyouwanttotell
everyoneinthegroup.Theproblemisthatnotwoofyouareinthesameroom,and
youmustcommunicateusingonlycellphones.What's
worseisthatduetoexcessiveusage,yourparentshaverefusedtopayyourcellphonebills,soyoumustdistributethenewsbycalling
eachotherinthecheapestpossibleway.Youwillcallseveralofyourfriends,theywillcallsomeoftheirfriends,andsoon
untileveryoneinthegrouphearsthenews.

Eachofyouisusingadifferentphoneserviceprovider,and
youknowthepriceofgirlAcallinggirlBforall
possibleAandB.Notallofyourfriendslikeeachother,andsomeofthem
willnevercallpeopletheydon'tlike.Yourjobistofindthecheapest
possiblesequenceofcallssothatthenewsspreadsfromyoutoalln-1
othermembersofthegroup.

Input
The
firstlineofinputgivesthenumberofcases,N(N<150).Ntestcasesfollow.Eachonestartswithtwo
linescontainingn(0<=n<=1000)andm(0
<=m<=40,000).Girlsarenumberedfrom0ton-1,
andyouaregirl0.Thenextmlineswilleachcontain3integers,u,
vandw,meaningthatacallfromgirlutogirlv
costswcents(0<=w<=1000).Noothercalls
arepossiblebecauseofgrudges,rivalriesandbecausetheyare,like,lame.
Theinputfilesizeisaround1200KB.

Output

Foreachtestcase,outputone
linecontaining"Case#x:"followedbythecostofthecheapest
methodofdistributingthenews.Ifthereisnosolution,print
"Possums!"instead.

SampleInputOutputfor
SampleInput

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<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>

usingnamespacestd;
typedeflonglongLL;
constdoubleinf=1e9+7;
constintN=1010;
constintM=40010;

structedge{
intu,v;
doublew;
}e[M];
intn,m;
doublex
,y
;
intpre
,id
,vis
,in
;
intg

;

intzhuliu(introot,intn,intm){
intres=0;intu,v;
while(1){
for(inti=0;i<n;++i)in[i]=inf;

for(inti=0;i<m;++i)
if(e[i].u!=e[i].v&&e[i].w<in[e[i].v]){
pre[e[i].v]=e[i].u;
in[e[i].v]=e[i].w;
}
for(inti=0;i<n;++i)
if(i!=root&&in[i]==inf)
return-1;
inttn=0;
memset(id,-1,sizeofid);
memset(vis,-1,sizeofvis);
in[root]=0;
for(inti=0;i<n;++i){
res+=in[i];
v=i;
while(vis[v]!=i&&id[v]==-1&&v!=root){
vis[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-1){
for(intu=pre[v];u!=v;u=pre[u])
id[u]=tn;
id[v]=tn++;
}
}
if(tn==0)break;//nocircle
for(inti=0;i<n;++i)if(id[i]==-1){
id[i]=tn++;
}
for(inti=0;i<m;){
v=e[i].v;
e[i].u=id[e[i].u];
e[i].v=id[e[i].v];
if(e[i].u!=e[i].v)
e[i++].w-=in[v];
else
swap(e[i],e[--m]);
}
n=tn;
root=id[root];
}
returnres;
}
intmain(){
//freopen("in.txt","r",stdin);
int_,cas=1;
scanf("%d",&_);
while(_--){
printf("Case#%d:",cas++);
scanf("%d%d",&n,&m);
for(inti=0;i<n;++i)
for(intj=0;j<n;++j)
g[i][j]=inf;
intu,v,cost;
while(m--){
scanf("%d%d%d",&u,&v,&cost);
if(u==v)continue;
g[u][v]=min(g[u][v],cost);
}
intL=0;
for(inti=0;i<n;++i)
for(intj=0;j<n;++j)
if(g[i][j]<inf){
e[L].u=i;
e[L].v=j;
e[L++].w=g[i][j];
}

intans=zhuliu(0,n,L);
if(ans==-1)
puts("Possums!");
else
printf("%d\n",ans);
}
}


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