您的位置:首页 > 其它

poj 3114 Countries in War(缩点+最短路)

2013-08-14 23:34 375 查看
[align=center]CountriesinWar[/align]

TimeLimit:1000MSMemoryLimit:65536K
TotalSubmissions:1932Accepted:607
Description

Intheyear2050,afterdifferentattemptsoftheUNtomaintainpeaceintheworld,thethirdworldwarbrokeout.Theimportanceofindustrial,commercialandmilitarysecretsobligesallthecountriestouseextremelysophisticatedespionageservices,
sothateachcityintheworldhasatleastonespyofeachcountry.Thesespiesneedtocommunicatewithotherspies,informersaswellastheirheadquartersduringtheiractions.Unluckilytheredoesn’texistasecurewayforaspytocommunicateduring
thewarperiod,thereforethemessagesarealwayssentincodesothatonlytheaddresseeisabletoreadthemessageandunderstanditsmeaning.

Thespiesusetheonlyservicethatfunctionsduringthewarperiod,thepost.Eachcityhasapostalagencywherethelettersaresent.Theletterscanbesentdirectlytotheirdestinationortootherpostalagencies,untiltheletterarrivesatthepostal
agencyofthedestinationcity,ifpossible.

ThepostalagencyincityAcansendaprintedlettertothepostalagencyincityBifthereisanagreementonsendingletters,whichdeterminesthetime,inhours,thatalettertakestoreachcityBfromcityA(andnotnecessarilytheopposite).If
thereisnoagreementbetweentheagenciesAandB,theagencyAcantrytosendthelettertoanyagencysothatthelettercanreachitsdestinationasearlyaspossible

Someagenciesareconnectedwithelectroniccommunicationmedia,suchassatellitesandopticalfibers.Beforethewar,theseconnectionscouldreachalltheagencies,makingthatalettercouldbesentinstantly.Butduringtheperiodofhostilitiesevery
countrystartstocontrolelectroniccommunicationandanagencycanonlysendalettertoanotheragencybyelectronicmedia(orinstantly)iftheyareinthesamecountry.Twoagencies,AandB,areinthesamecountryifaprintedlettersentfromanyone
oftheagenciescanbedeliveredtotheotherone.

Theespionageserviceofyourcountryhasmanagedtoobtainthecontentofalltheagreementsonsendingmessagesexistingintheworldanddesirestofindouttheminimumtimetosendaletterbetweendifferentpairsofcities.Areyoucapableofhelping
them?

Input

Theinputcontainsseveraltestcases.Thefirstlineofeachtestcasecontainstwointegerseparatedbyaspace,
N(1≤N≤500)andE(0≤E≤N2),indicatingthenumbersofcities(numberedfrom1to
N)andofagreementsonsendingmessages,respectively.Followingthem,then,
Elines,eachcontainingthreeintegersseparatedbyspaces,X,
YandH(1≤X,Y≤N,1≤H≤1000),indicatingthatthereexistanagreementtosendaprintedletterfromcity
XtocityY,andthatsuchaletterwillbedeliveredinHhours.

Afterthat,therewillbealinewithanintegerK(0≤K≤100),thenumberofqueries.Finally,therewillbe
Klines,eachrepresentingaqueryandcontainingtwointegersseparatedbyaspace,
OandD(1≤O,D≤N).Youmustdeterminetheminimumtimetosendaletterfromcity
OtocityD.

TheendoftheinputisindicatedbyN=0.

Output

ForeachtestcaseyourprogramshouldproduceKlinesofoutput.The
I-thlineshouldcontainanintegerM,theminimumtime,inhours,tosendaletterinthe
I-thquery.Iftherearen’tcommunicationmediabetweenthecitiesofthequery,youshouldprint“Naoepossivelentregaracarta”(“It’simpossibletodelivertheletter”).

Printablanklineaftereachtestcase.

SampleInput
45
125
2110
348
437
236
5
12
13
14
43
41
33
1210
231
321
3
13
31
32
00

SampleOutput
0
6
6
0
Naoepossivelentregaracarta

10
Naoepossivelentregaracarta
0


题意:给出一个有向图,若两个点在同一强连通分量中,则这两个点的距离为0.给出若干询问uv,求u到v的距离.

思路:先缩点,然后求最短路,floyd超时,所以用dij。


AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<map>
#defineL(rt)(rt<<1)
#defineR(rt)(rt<<1|1)
#definelllonglong
usingnamespacestd;

constintmaxn=505;
constdoubleINF=1000000000;

structnode
{
intv,w,next;
}edge[maxn*maxn];
inthead[maxn],scc[maxn],G[maxn][maxn],dis[maxn];
intlow[maxn],dfn[maxn],stack[maxn],sum[maxn];
boolins[maxn],vis[maxn];
intn,m,num,cnt,snum,top;
voidinit()
{
memset(head,-1,sizeof(head));
num=0;
}
voidadd(intu,intv,intw)
{
edge[num].v=v;
edge[num].w=w;
edge[num].next=head[u];
head[u]=num++;
}
voiddfs(intu)
{
intx;
dfn[u]=low[u]=++cnt;
stack[top++]=u;
ins[u]=true;
for(inti=head[u];i!=-1;i=edge[i].next)
{
intv=edge[i].v;
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
elseif(ins[v])low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
snum++;
do{
x=stack[--top];
ins[x]=false;
scc[x]=snum;
}while(x!=u);
}
}
voidtarjan()
{
memset(ins,false,sizeof(ins));
memset(dfn,0,sizeof(dfn));
snum=top=cnt=0;
for(inti=1;i<=n;i++)
if(!dfn[i])dfs(i);
}
voidmake_graph()
{
for(inti=1;i<=n;i++)
for(intj=1;j<=n;j++)
G[i][j]=INF;
for(intu=1;u<=n;u++)
for(inti=head[u];i!=-1;i=edge[i].next)
{
intv=edge[i].v;
if(scc[u]==scc[v])G[u][v]=0;
elseG[u][v]=edge[i].w;
}
}
voiddij(intu,intv)
{
intmm,x;
memset(vis,false,sizeof(vis));
for(inti=1;i<=n;i++)
dis[i]=G[u][i];
dis[u]=0;
vis[u]=true;
for(inti=1;i<=n;i++)
{
mm=INF;
for(intj=1;j<=n;j++)
if(!vis[j]&&dis[j]<mm)mm=dis[x=j];
if(mm==INF)break;
vis[x]=true;
for(intj=1;j<=n;j++)
if(!vis[j]&&dis[j]>dis[x]+G[x][j])
dis[j]=dis[x]+G[x][j];
}
if(dis[v]==INF)printf("Naoepossivelentregaracarta\n");
elseprintf("%d\n",dis[v]);
}
intmain()
{
intk,a,b,c;
while(scanf("%d%d",&n,&m),n)
{
init();
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
tarjan();
make_graph();
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&a,&b);
dij(a,b);
}
printf("\n");
}
return0;
}

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