UVa11183 - Teen Girl Squad(最小树形图-裸)
2014-08-05 13:00
495 查看
ProblemI
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.
#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;
}
TeenGirlSquad
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 |
#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;
}
相关文章推荐
- uva11183 Teen Girl Squad(最小树形图)
- UVA 11183 Teen Girl Squad(最小树形图)
- Uva 11183 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(最小树形图模板)
- UVA11183 Teen Girl Squad (最小树形图)
- UVA 11183 Teen Girl Squad(最小树形图裸题)
- UVA 11183 Teen Girl Squad(最小树形图)
- kuangbin专题八 UVA11183 Teen Girl Squad(最小树形图)
- uva11183 Teen Girl Squad(最小树形图)
- uva 11183 Teen Girl Squad (最小树形图)
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
- UVA 11183 Teen Girl Squard(最小树形图)
- UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)
- UVA 11183 Teen Girl Squad (最小树形图)
- Teen Girl Squad - UVA 11183 - 最小树形图
- 【UVa】11183 Teen Girl Squad 最小树形图