标签:
4 1 2 4 10 2 0 1 1 2 2 0 5 6 3 1 2
3
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 const int maxn = 310; 7 int n,mp[maxn][maxn],d[maxn]; 8 int spfa(int s,int t,int &val) { 9 queue<int>q; 10 bool inq[maxn] = {false}; 11 memset(d,0x3f,sizeof d); 12 q.push(s); 13 d[s] = 0; 14 val = INT_MAX; 15 while(!q.empty()) { 16 int u = q.front(); 17 q.pop(); 18 inq[u] = false; 19 for(int i = 0; i < n; ++i) { 20 if(u != s && i == s) val = min(val,d[u]+mp[u][i]); 21 if(d[i] > d[u] + mp[u][i]) { 22 d[i] = d[u] + mp[u][i]; 23 if(!inq[i]) { 24 inq[i] = true; 25 q.push(i); 26 } 27 } 28 } 29 } 30 return d[t]; 31 } 32 int main() { 33 while(~scanf("%d",&n)) { 34 for(int i = 0; i < n; ++i) 35 for(int j = 0; j < n; ++j) 36 scanf("%d",mp[i]+j); 37 int a,b; 38 int ret = spfa(0,n-1,a); 39 spfa(n-1,0,b); 40 printf("%d\n",min(a+b,ret)); 41 } 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4668781.html