标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9052 Accepted Submission(s): 2383
/* ID: LinKArftc PROG: 1385.cpp LANG: C++ */ #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <string> #include <utility> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const double e = exp(1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 100; int mp[maxn][maxn]; int path[maxn][maxn];//path[i][j]记录从i到j的下一个点 int tax[maxn]; int n, s, t; void floyd() { for (int k = 1; k <= n; k ++) { for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { if (i == k || j == k) continue; int tmp = mp[i][k] + mp[k][j] + tax[k]; if (mp[i][j] > tmp) { mp[i][j] = tmp; path[i][j] = path[i][k]; } else if (mp[i][j] == tmp) { path[i][j] = min(path[i][j], path[i][k]); } } } } } void print_path() { printf("Path: %d", s); int cur = s; while (cur != t) { cur = path[cur][t]; printf("-->%d", cur); } printf("\n"); } int main() { while (~scanf("%d", &n) && n) { for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { scanf("%d", &mp[i][j]); if (mp[i][j] == -1) mp[i][j] = inf;//最好用inf替换,用-1的话特判很容易错 else path[i][j] = j; } } for (int i = 1; i <= n; i ++) scanf("%d", &tax[i]); floyd(); while (~scanf("%d %d", &s, &t)) { if (s == -1 && t == -1) break; printf("From %d to %d :\n", s, t); print_path(); printf("Total cost : %d\n\n", mp[s][t]); } } return 0; }
HDU1385 Minimum Transport Cost
标签:
原文地址:http://www.cnblogs.com/LinKArftc/p/4902708.html