您的位置:首页 > 其它

poj 1502 MPI Maelstrom

2013-04-26 10:12 197 查看
[align=center]MPIMaelstrom[/align]

TimeLimit:1000MSMemoryLimit:10000K
TotalSubmissions:3906Accepted:2339
Description
BIThasrecentlytakendeliveryoftheirnewsupercomputer,a32processorApolloOdysseydistributedsharedmemorymachinewithahierarchicalcommunicationsubsystem.ValentineMcKee'sresearchadvisor,JackSwigert,hasasked
hertobenchmarkthenewsystem.

``SincetheApolloisadistributedsharedmemorymachine,memoryaccessandcommunicationtimesarenotuniform,''ValentinetoldSwigert.``Communicationisfastbetweenprocessorsthatsharethesamememorysubsystem,butitisslowerbetweenprocessors
thatarenotonthesamesubsystem.CommunicationbetweentheApolloandmachinesinourlabissloweryet.''

``HowisApollo'sportoftheMessagePassingInterface(MPI)workingout?''Swigertasked.

``Notsowell,''Valentinereplied.``Todoabroadcastofamessagefromoneprocessortoalltheothern-1processors,theyjustdoasequenceofn-1sends.Thatreallyserializesthingsandkillstheperformance.''

``Isthereanythingyoucandotofixthat?''

``Yes,''smiledValentine.``Thereis.Oncethefirstprocessorhassentthemessagetoanother,thosetwocanthensendmessagestotwootherhostsatthesametime.Thentherewillbefourhoststhatcansend,andsoon.''

``Ah,soyoucandothebroadcastasabinarytree!''

``Notreallyabinarytree--therearesomeparticularfeaturesofournetworkthatweshouldexploit.Theinterfacecardswehavealloweachprocessortosimultaneouslysendmessagestoanynumberoftheotherprocessorsconnectedtoit.However,themessages
don'tnecessarilyarriveatthedestinationsatthesametime--thereisacommunicationcostinvolved.Ingeneral,weneedtotakeintoaccountthecommunicationcostsforeachlinkinournetworktopologiesandplanaccordinglytominimizethetotaltime
requiredtodoabroadcast.''
Input
Theinputwilldescribethetopologyofanetworkconnectingnprocessors.Thefirstlineoftheinputwillben,thenumberofprocessors,suchthat1<=n<=100.

Therestoftheinputdefinesanadjacencymatrix,A.Theadjacencymatrixissquareandofsizenxn.Eachofitsentrieswillbeeitheranintegerorthecharacterx.ThevalueofA(i,j)indicatestheexpenseofsendingamessagedirectlyfromnodeito
nodej.AvalueofxforA(i,j)indicatesthatamessagecannotbesentdirectlyfromnodeitonodej.

Notethatforanodetosendamessagetoitselfdoesnotrequirenetworkcommunication,soA(i,i)=0for1<=i<=n.Also,youmayassumethatthenetworkisundirected(messagescangoineitherdirectionwithequaloverhead),sothatA(i,j)=A(j,i).Thus
onlytheentriesonthe(strictly)lowertriangularportionofAwillbesupplied.

TheinputtoyourprogramwillbethelowertriangularsectionofA.Thatis,thesecondlineofinputwillcontainoneentry,A(2,1).Thenextlinewillcontaintwoentries,A(3,1)andA(3,2),andsoon.

Output
Yourprogramshouldoutputtheminimumcommunicationtimerequiredtobroadcastamessagefromthefirstprocessortoalltheotherprocessors.

SampleInput
5
50
305
1002050
10xx10

SampleOutput
35


题意:求起点到所有点的距离的最大值,用dij算法


AC代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#defineN105
#defineINF10000000

usingnamespacestd;
intmap

,dist
,v
;
intn;
voiddij()
{
memset(v,0,sizeof(v));
for(inti=1;i<=n;i++)
dist[i]=map[1][i];
v[1]=1;
dist[1]=0;
for(inti=1;i<=n;i++)
{
intx,m=INF;
for(intj=1;j<=n;j++)
if(!v[j]&&dist[j]<m)
m=dist[x=j];
if(m==INF)break;
v[x]=1;
for(intj=1;j<=n;j++)
if(!v[j]&&dist[j]>dist[x]+map[x][j])
dist[j]=dist[x]+map[x][j];
}
}
intmain()
{
strings;
cin>>n;
getchar();
for(inti=2;i<=n;i++)
{for(intj=1;j<i;j++)
{
cin>>s;
if(s[0]=='x')
map[i][j]=map[j][i]=INF;
else
{
intnum=0;
for(intk=0;k<s.size();k++)
{
num*=10;
num+=s[k]-'0';
}
map[i][j]=map[j][i]=num;
}
}
map[i][i]=0;
}
dij();
intans=0;
for(inti=1;i<=n;i++)
{
if(ans<dist[i])
ans=dist[i];
}
cout<<ans<<endl;
return0;

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