Created
March 7, 2015 14:10
-
-
Save ik11235/c2c3e35fa8258270f658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define INF 100000000 | |
using namespace std; | |
int main(int argc, char *argv[]) | |
{ | |
int n,m; | |
cin>>n>>m; | |
long long int edge[n][n]; | |
for(int i=0;i<n;i++) | |
{ | |
for(int j=0;j<n;j++) | |
edge[i][j]=INF; | |
edge[i][i]=0; | |
} | |
for(int i=0;i<m;i++) | |
{ | |
int a,b,c; | |
cin>>a>>b>>c; | |
a--,b--; | |
edge[a][b]=c; | |
edge[b][a]=c; | |
} | |
for(int k=0;k<n;k++) | |
for(int i=0;i<n;i++) | |
for(int j=0;j<n;j++) | |
edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]); | |
int k; | |
cin>>k; | |
long long int sum=0; | |
for(int i=0;i<n;i++) | |
for(int j=i+1;j<n;j++) | |
sum+=edge[i][j]; | |
for(int i=0;i<k;i++) | |
{ | |
int x,y,z; | |
cin>>x>>y>>z; | |
x--,y--; | |
if(edge[x][y]>z) | |
{ | |
edge[x][y]=z; | |
edge[y][x]=z; | |
sum=0; | |
for(int i=0;i<n;i++) | |
for(int j=0;j<n;j++) | |
{ | |
edge[i][j]=min(edge[i][j],edge[i][x]+edge[x][y]+edge[y][j]); | |
edge[i][j]=min(edge[i][j],edge[i][y]+edge[y][x]+edge[x][j]); | |
if(i<j) | |
sum+=edge[i][j]; | |
} | |
} | |
cout<<sum<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment