1 #include<bits/stdc++.h> 2 #define ll long long 3 #define scan(i) scanf("%d",&i) 4 #define scand(i) scanf("%lf",&i) 5 #define scanl(i) scanf("%lld",&i) 6 #define f(i,a,b) for(int i=a;i<=b;i++) 7 #define pb(i) push_back(i) 8 #define ppb pop_back() 9 #define pf printf 10 #define dbg(args...) cout<<#args<<" : "<<args<<endl; 11 using namespace std; 12 int t,n,m; 13 int dis[302][302]; 14 int x,y,w; 15 void floyd(){ 16 f(k,0,n-1){ 17 f(i,0,n-1){ 18 f(j,0,n-1){ 19 if(i==j) dis[i][j]=0; 20 else dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 21 } 22 } 23 } 24 } 25 int main(){ 26 scan(t); 27 f(kk,1,t){ 28 scanf("%d%d",&n,&m); 29 memset(dis,0x3f,sizeof(dis)); 30 f(i,1,m){ 31 scanf("%d%d%d",&x,&y,&w); 32 dis[x][y]=w; 33 } 34 floyd(); 35 f(i,1,6){ 36 scanf("%d%d",&x,&y); 37 pf("%d\n",-dis[y][x]); 38 dis[x][y]=-dis[y][x]; 39 if(i-6) floyd(); 40 } 41 //pf("Case #%d: %lld,kk,ans); 42 } 43 return 0; 44 }
算法复杂度O(n^2)。