UVa383 - Shipping Routes

来源:互联网 发布:xml json namespace 编辑:程序博客网 时间:2024/05/17 08:17
#include <cstdio>#include <map>#include <iostream>#include <fstream>#include <string>#include <algorithm>using namespace std;const int N = 35;const int INF = 0x3f3f3f3f;int cnt;int f[N][N];map<string, int> strMap;void floyd_warshall(int n);int main(){    int m, n, p;    int t;    string warehouse;    string s1, s2;    int size;    int cost;#ifndef ONLINE_JUDGE    //freopen("uva_in.txt", "r", stdin);    fstream cin("uva_in.txt");#endif    //scanf("%d", &t);    cout << "SHIPPING ROUTES OUTPUT" << endl << endl;;    cin >> t;    for (int k = 0; k < t; k++) {        cout << "DATA SET  " << k + 1 << endl << endl;;        strMap.clear();        cnt = 0;        //scanf("%d%d%d", &m, &n, &p);        cin >> m >> n >> p;        for (int i = 0; i < m; i++) {            cin >> warehouse;            if (!strMap.count(warehouse))                 strMap[warehouse] = cnt++;        }                    for (int i = 0; i < m; i++) {           for (int j = 0; j < m; j++) {                if (i == j) f[i][j] = 0;                else f[i][j] = INF;           }         }        for (int i = 0; i < n; i++) {            cin >> s1 >> s2;            int u = strMap[s1];            int v = strMap[s2];            f[u][v] = f[v][u] = 1;        }        floyd_warshall(m);        for (int i = 0; i < p; i++) {            cin >> size >> s1 >> s2;            int u = strMap[s1];            int v = strMap[s2];            if (f[u][v] == INF) {                cout << "NO SHIPMENT POSSIBLE" << endl;            } else {                cout << "$" << size * f[u][v] * 100 << endl;            }        }         cout << endl;    }    cout << "END OF OUTPUT" << endl;    return 0;}void floyd_warshall(int n) {    for (int k = 0; k < n; k++) {        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                f[i][j] = min(f[i][j], f[i][k] + f[k][j]);            }        }    }}

原创粉丝点击