您的位置:首页 > 其它

USACO Section 3.2 Sweet Butter (butter)

2013-02-13 16:17 405 查看

SweetButter

GregGalperin--2001
FarmerJohnhasdiscoveredthesecrettomakingthesweetestbutterinallofWisconsin:sugar.Byplacingasugarcubeoutinthepastures,heknowstheN(1<=N<=500)cowswilllickitandthuswillproducesuper-sweetbutterwhichcanbemarketedatbetterprices.Ofcourse,hespendstheextramoneyonluxuriesforthecows.

FJisaslyfarmer.LikePavlovofold,heknowshecantrainthecowstogotoacertainpasturewhentheyhearabell.Heintendstoputthesugarthereandthenringthebellinthemiddleoftheafternoonsothattheevening'smilkingproducesperfectmilk.

FJknowseachcowspendshertimeinagivenpasture(notnecessarilyalone).Giventhepasturelocationofthecowsandadescriptionofthepathstheconnectthepastures,findthepastureinwhichtoplacethesugarcubesothatthetotaldistancewalkedbythecowswhenFJringsthebellisminimized.FJknowsthefieldsareconnectedwellenoughthatsomesolutionisalwayspossible.

PROGRAMNAME:butter

INPUTFORMAT

Line1:Threespace-separatedintegers:N,thenumberofpastures:P(2<=P<=800),andthenumberofconnectingpaths:C(1<=C<=1,450).Cowsareuniquelynumbered1..N.Pasturesareuniquelynumbered1..P.

Lines2..N+1:Eachlinecontainsasingleintegerthatisthepasturenumberinwhichacowisgrazing.Cowi'spastureislistedonlinei+1.

LinesN+2..N+C+1:Eachlinecontainsthreespace-separatedintegersthatdescribeasinglepaththatconnectsapairofpasturesanditslength.Pathsmaybetraversedineitherdirection.Nopairofpasturesisdirectlyconnectedbymorethanonepath.Thefirsttwointegersareintherange1..P;thethirdintegerisintherange(1..225).

SAMPLEINPUT(filebutter.in)

345
2
3
4
121
135
237
243
345

INPUTDETAILS

Thisdiagramshowstheconnectionsgeometrically:

P2
P1@--1--@C1
\|\
\|\
573
\|\
\|\C3
C2@--5--@
P3P4

OUTPUTFORMAT

Line1:Asingleintegerthatistheminimumdistancethecowsmustwalktoapasturewithasugarcube.

SAMPLEOUTPUT(filebutter.out)

8

OUTPUTDETAILS:

Puttingthecubeinpasture4means:cow1walks3units;cow2walks5
units;cow3walks0units--atotalof8.

思路:我是枚举牧场,然后spfa,求最短距离。加起来就是了,唉,又用了queue

Executing...
Test1:TESTOK[0.000secs,12928KB]
Test2:TESTOK[0.000secs,12928KB]
Test3:TESTOK[0.000secs,12928KB]
Test4:TESTOK[0.000secs,12928KB]
Test5:TESTOK[0.000secs,12928KB]
Test6:TESTOK[0.011secs,12928KB]
Test7:TESTOK[0.032secs,12928KB]
Test8:TESTOK[0.065secs,12928KB]
Test9:TESTOK[0.108secs,12928KB]
Test10:TESTOK[0.119secs,12928KB]

AlltestsOK.


/*
ID:wuhuaju2
PROG:butter
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
usingnamespacestd;

constintINF=1000000;
constintmaxp=810,maxm=1510;
intd[maxp],tot[maxm],dis[maxp][maxm],poi[maxp][maxm];
boolvis[maxp];
inta[maxp];
intn,p,m,x,y,z,ans,sum;

queue<int>q;

voidclose()
{
fclose(stdin);
fclose(stdout);
exit(0);
}

voidspfa(ints)
{
memset(vis,false,sizeof(vis));
while(!q.empty())
q.pop();
for(inti=1;i<=p;i++)
d[i]=INF;
d[s]=0;
q.push(s);
vis[s]=true;
while(!q.empty())
{
s=q.front();
q.pop();
for(inti=1;i<=tot[s];i++)
{
if(d[poi[s][i]]>d[s]+dis[s][i])
{
d[poi[s][i]]=d[s]+dis[s][i];
if(notvis[poi[s][i]])
{
q.push(poi[s][i]);
vis[poi[s][i]]=true;
}
}
}
vis[s]=false;
}
}

voidwork()
{
//for(inti=1;i<=p;i++)
ans=INF;
for(inti=1;i<=p;i++)
{
sum=0;
spfa(i);
//cout<<"p:"<<i<<endl;
for(intj=1;j<=n;j++)
sum+=d[a[j]];
//cout<<sum<<endl;
if(sum<ans)
ans=sum;
}
printf("%d\n",ans);
}

voidinit()
{
freopen("butter.in","r",stdin);
freopen("butter.out","w",stdout);
scanf("%d%d%d",&n,&p,&m);
memset(tot,0,sizeof(tot));
for(inti=1;i<=n;i++)
scanf("%d",&a[i]);
for(inti=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
tot[x]++;
dis[x][tot[x]]=z;
poi[x][tot[x]]=y;
tot[y]++;
dis[y][tot[y]]=z;
poi[y][tot[y]]=x;//wuxiangtu
}
}

intmain()
{
init();
work();
close();
return0;
}





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