HDU 2112

#include #include #include #include #include #include #include #include #include #define MAXN 205 #define MAXM 10005 #define MOD 10000 #define inf 1000000000 #define eps 1e-9 #define pi acos(-1.0) typedef long long LL; using namespace std; int n, mat[MAXN][MAXN], dis[MAXN], st, ed, w, idx; bool vis[MAXN]; char s[35], d[35]; map mp; void Dijkstra() { memset(vis, false, sizeof(vis)); for (int i = 1; i <= idx; i++) dis[i] = inf; dis[st] = 0; for (int i = 1; i < idx; i++) { int k = -1; for (int j = 1; j <= idx; j++) if (!vis[j] && (k == -1 || dis[j] < dis[k])) k = j; if (k == -1) break; vis[k] = true; for (int j = 1; j <= idx; j++) if (!vis[j] && mat[k][j] && mat[k][j] + dis[k] < dis[j]) dis[j] = dis[k] + mat[k][j]; } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while ((cin >> n) && (n + 1)) { mp.clear(); cin >> s >> d; idx = 0; if (mp[s] == 0) mp[s] = ++idx; if (mp[d] == 0) mp[d] = ++idx; st = mp[s], ed = mp[d]; memset(mat, 0, sizeof(mat)); for (int i = 0; i < n; i++) { cin >> s >> d >> w; if (mp[s] == 0) mp[s] = ++idx; if (mp[d] == 0) mp[d] = ++idx; if (mat[mp[s]][mp[d]] == 0 || mat[mp[s]][mp[d]] > w) mat[mp[s]][mp[d]] = mat[mp[d]][mp[s]] = w; } Dijkstra(); if (dis[ed] == inf) cout << -1 << endl; else cout << dis[ed] << endl; } return 0; }

posted on 2012-11-28 13:56  Sure_Yi  阅读(134)  评论(0编辑  收藏  举报

导航