您的位置:首页 > 其它

Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

2015-08-24 19:53 405 查看
C.RoadsinBerland

timelimitpertest
2seconds

memorylimitpertest
256megabytes

input
standardinput

output
standardoutput

Therearencitiesnumberedfrom1toninBerland.Someofthemareconnectedbytwo-wayroads.Eachroadhasitsownlength—anintegernumberfrom1to1000.Itisknownthatfromeachcityitispossibletogettoanyothercitybyexistingroads.Alsoforeachpairofcitiesitisknowntheshortestdistancebetweenthem.BerlandGovernmentplanstobuildknewroads.Foreachoftheplannedroaditisknownitslength,andwhatcitiesitwillconnect.Tocontrolthecorrectnessoftheconstructionofnewroads,aftertheopeningofanotherroadBerlandgovernmentwantstocheckthesumoftheshortestdistancesbetweenallpairsofcities.Helpthem—foragivenmatrixofshortestdistancesontheoldroadsandplansofallnewroads,findouthowthesumoftheshortestdistancesbetweenallpairsofcitieschangesafterconstructionofeachroad.

Input
Thefirstlinecontainsintegern(2 ≤ n ≤ 300)—amountofcitiesinBerland.Thentherefollownlineswithnintegernumberseach—thematrixofshortestdistances.j-thintegerinthei-throw—di, j,theshortestdistancebetweencitiesiandj.Itisguaranteedthatdi, i = 0, di, j = dj, i,andagivenmatrixisamatrixofshortestdistancesforsomesetoftwo-wayroadswithintegerlengthsfrom1to1000,suchthatfromeachcityitispossibletogettoanyothercityusingtheseroads.

Nextlinecontainsintegerk(1 ≤ k ≤ 300)—amountofplannedroads.Followingklinescontainthedescriptionoftheplannedroads.Eachroadisdescribedbythreespace-separatedintegersai,bi,ci(1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000)—aiandbi—pairofcities,whichtheroadconnects,ci—thelengthoftheroad.Itcanbeseveralroadsbetweenapairofcities,butnoroadconnectsthecitywithitself.

Output
Outputkspace-separatedintegersqi(1 ≤ i ≤ k).qishouldbeequaltothesumofshortestdistancesbetweenallpairsofcitiesaftertheconstructionofroadswithindexesfrom1toi.Roadsarenumberedfrom1intheinputorder.Eachpairofcitiesshouldbetakenintoaccountinthesumexactlyonce,i.e.wecountunorderedpairs.

Sampletest(s)

input
2
05
50
1
123


output
3


input
3
045
409
590
2
238
121


output
1712

题目大意:给出一个矩阵,表示一个图和边权值(这里的边权值已经是两点间的最短距离)。然后给出K次修改,对于每一次边权修改,输出修改后任意两点的最小距离的和。

Floyd变形。首先任意两点的最小距离的和只能暴力求解,对于边的更新,我们肯定会想到跑一边floyd但这会超时。

对于第修改的边a,b我们可以知道任意两点i,j的最短距离要么经过a-b,要么经过b-a,要么不经过a点也不经过b点。比n^3要好一些.

dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][a]+dis[b][j]+c);
dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][b]+dis[a][j]+c);


/************************************************
Author:pk29
CreatedTime:2015/8/238:53:43
FileName:4.cpp
*************************************************/
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<stack>
#defineullunsignedlonglong
#definelllonglong
#definemod90001
#defineINF0x3f3f3f3f
#definemaxn10000+10
#definecle(a)memset(a,0,sizeof(a))
constullinf=1LL<<61;
constdoubleeps=1e-5;
usingnamespacestd;

boolcmp(inta,intb){
returna>b;
}
intdis[310][310];
intmain()
{
#ifndefONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
intn,k;
cin>>n;
for(inti=1;i<=n;i++)
for(intj=1;j<=n;j++){
scanf("%d",&dis[i][j]);
}
cin>>k;
inta,b,c;
while(k--){
scanf("%d%d%d",&a,&b,&c);
if(c<dis[a][b]){
for(inti=1;i<=n;i++)
for(intj=1;j<=n;j++){
dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][a]+dis[b][j]+c); dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][b]+dis[a][j]+c);}
}
llsum=0;
for(inti=1;i<=n;i++)
for(intj=i+1;j<=n;j++){
sum+=dis[i][j];
}
cout<<sum<<"";
}
return0;
}



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