看数据不太大,200,满足n^3的复杂度下很快的跑完,;
所以写起来Floyd特别快;
Floyd实质是区间DP;
错点:我状态转移方程写错了,如果顺的话是k , i, j ; dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
但是理解不够深刻,写成了i,j,k;还用了上式;是错误的;
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40756 Accepted Submission(s): 15076
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
AC代码:
#include <bits/stdc++.h> #define INF 9999999 using namespace std ; int dis[205][205]; int u , v,s,t; void warsh() { int i , j , k ; for(k=0;k<v;k++) { for(i=0;i<v;i++) { for(j=0;j<v;j++) { dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } } } } int main() { while(cin>>v>>u) { for(int i = 0 ; i <v;i++) { for(int j = 0 ; j <v ; j++) { dis[i][j]=INF; } dis[i][i]=0; } for(int i = 0;i<u;i++) { int a , b , c; cin>>a>>b>>c; if(dis[a][b]>c) dis[a][b]=dis[b][a]=c; } cin>>s>>t; warsh(); if(dis[s][t]==INF) printf("-1\n"); else printf("%d\n",dis[s][t]); } return 0 ; }