您的位置:首页 > 其它

POJ 1125 Stockbroker Grapevine 图论 Floyd-Washall算法

2013-09-04 19:44 447 查看
StockbrokerGrapevine

TimeLimit:1000MSMemoryLimit:10000K
TotalSubmissions:23914Accepted:13143
Description
Stockbrokersareknowntooverreacttorumours.Youhavebeencontractedtodevelopamethodofspreadingdisinformationamongstthestockbrokerstogiveyouremployerthetacticaledgeinthestockmarket.Formaximumeffect,
youhavetospreadtherumoursinthefastestpossibleway.

Unfortunatelyforyou,stockbrokersonlytrustinformationcomingfromtheir"Trustedsources"Thismeansyouhavetotakeintoaccountthestructureoftheircontactswhenstartingarumour.Ittakesacertainamountoftimeforaspecificstockbrokertopass
therumourontoeachofhiscolleagues.Yourtaskwillbetowriteaprogramthattellsyouwhichstockbrokertochooseasyourstartingpointfortherumour,aswellasthetimeitwilltakefortherumourtospreadthroughoutthestockbrokercommunity.
Thisdurationismeasuredasthetimeneededforthelastpersontoreceivetheinformation.
Input
Yourprogramwillinputdatafordifferentsetsofstockbrokers.Eachsetstartswithalinewiththenumberofstockbrokers.Followingthisisalineforeachstockbrokerwhichcontainsthenumberofpeoplewhotheyhavecontact
with,whothesepeopleare,andthetimetakenforthemtopassthemessagetoeachperson.Theformatofeachstockbrokerlineisasfollows:Thelinestartswiththenumberofcontacts(n),followedbynpairsofintegers,onepairforeachcontact.Each
pairlistsfirstanumberreferringtothecontact(e.g.a'1'meanspersonnumberoneintheset),followedbythetimeinminutestakentopassamessagetothatperson.Therearenospecialpunctuationsymbolsorspacingrules.

Eachpersonisnumbered1throughtothenumberofstockbrokers.Thetimetakentopassthemessageonwillbebetween1and10minutes(inclusive),andthenumberofcontactswillrangebetween0andonelessthanthenumberofstockbrokers.Thenumberof
stockbrokerswillrangefrom1to100.Theinputisterminatedbyasetofstockbrokerscontaining0(zero)people.

Output
Foreachsetofdata,yourprogrammustoutputasinglelinecontainingthepersonwhoresultsinthefastestmessagetransmission,andhowlongbeforethelastpersonwillreceiveanygivenmessageafteryougiveittothisperson,
measuredinintegerminutes.

Itispossiblethatyourprogramwillreceiveanetworkofconnectionsthatexcludessomepersons,i.e.somepeoplemaybeunreachable.Ifyourprogramdetectssuchabrokennetwork,simplyoutputthemessage"disjoint".Notethatthetimetakentopassthe
messagefrompersonAtopersonBisnotnecessarilythesameasthetimetakentopassitfromBtoA,ifsuchtransmissionispossibleatall.

SampleInput
3
22435
21236
21222
5
3442853
158
4164102752
0
22515
0

SampleOutput
32
310


这道题个人感觉英文部分比ACM部分难,看的纠结死了,但是一理解题意,发现题目其实是最基础,最简单的图论题,题目意思就是,有个人,唯恐股票市场不乱,然后给股票投资人一些假情报,造成每个人疯狂,然后,这些股票投资人也不是吃干饭的,他们只相信一部分自己相信的其他股票投资人的信息,并且他们花一定时间会让这个股票投资人相信自己的假情报,现在求这个谁先知道这个假情报会使用最短时间,让其他所有投资者相信这个假情报呢???如果不能让所有人相信,输出“disjoint”。这个题解法我想的还是Floyd-Washall算法求出全源最短路径,每个股票投资人是之间的一个点,然后从相信的股票投资人那里获取消息就是路径,权值为消息传播时间,先将所有路径的距离定义为最大,然后建立图,并且带入Floyd-Washall算法模板计算出任意两个股票投资人之间传播消息的最短时间,之后计算一个人到其他所有人中传播最短时间的最大值,这个值就是这个人把信息传播给其他所有人的最小值,然后将所有人传播时间的最小值中继续取最小值,即为这个消息传播到每一个人所需要的最小时间,如果这个时间大于初始化的极大值,则证明消息无法传达给所有人,输出“disjoint”,否则,输出第一个传播人以及最小时间,此题就可以AC了。


下面是AC代码:


#include<cstdio>
#include<iostream>
usingnamespacestd;
intD[105][105],G[105][105];
voidFloyd_Washall(intn)
{
inti,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
D[i][j]=G[i][j];
for(i=1;i<=n;i++){D[i][i]=0;}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j&&D[i][j]>D[i][k]+D[k][j])
D[i][j]=D[i][k]+D[k][j];
}
intmain()
{
inti,j,n,m,a,b,max,min,pmin;
while(1)
{
scanf("%d",&n);
if(n==0)
break;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
G[i][j]=1000;
for(i=1;i<=n;i++)
{
cin>>m;
for(j=1;j<=m;j++)
{
cin>>a>>b;
G[i][a]=b;
}
}
Floyd_Washall(n);
min=1000;
for(i=1;i<=n;i++)
{
max=0;
for(j=1;j<=n;j++)
{
if(max<D[i][j]&&i!=j)
max=D[i][j];
}
if(min>max)
{
min=max;
pmin=i;
}
}
if(min<1000)
printf("%d%d\n",pmin,min);
else
printf("disjoint\n");
}
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: