USACO Section 2.4: Bessie Come Home

时间:2023-12-22 17:26:44

因为题目给了边的信息,所以比较适用bell-man的方法

 /*
 ID: yingzho1
 LANG: C++
 TASK: comehome
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <map>
 #include <vector>
 #include <set>
 #include <algorithm>
 #include <stdio.h>
 #include <queue>
 #include <cstring>
 #include <cmath>
 #include <list>

 using namespace std;
 #define inf 10000000

 ifstream fin("comehome.in");
 ofstream fout("comehome.out");

 int N;
 struct edge {
     char s, d;
     int e;
     edge(int a, int b, int c) : s(a), d(b), e(c) { }
     edge() : s(), d(), e() { }
 };
 map<char, int> dis;
 ;

 int main()
 {
     fin >> N;
     vector<edge> path(*N);

     ; i < N; i++) {
         char s, d;
         int e;
         fin >> s >> d >> e;
         path[edgenum].s = s;
         path[edgenum].d = d;
         path[edgenum++].e = e;
         path[edgenum].s = d;
         path[edgenum].d = s;
         path[edgenum++].e = e;
     }
     //for (int i = 0; i < edgenum; i++) {
         //cout << "s: " << path[i].s << ", d: " << path[i].d << ", e: " << path[i].e << endl;
     //}
     for (char i = 'a'; i <= 'z'; i++) dis[i] = inf;
     for (char i = 'A'; i <= 'Z'; i++) dis[i] = inf;
     dis[;
     bool flag;
     ; i < edgenum; i++) {
         flag = false;
         ; j < edgenum; j++) {
             if (dis[path[j].d] > dis[path[j].s]+path[j].e) {
                 dis[path[j].d] = dis[path[j].s]+path[j].e;
                 flag = true;
                 //cout << "dis[" << path[j].d << "]: " << dis[path[j].d] << endl;
             }
         }
         if (!flag) break;
     }
     int min_path = inf;
     char min_index;
     ; i < edgenum; i++) {
         if (path[i].s >= 'A' && path[i].s < 'Z' && min_path > dis[path[i].s]) {
             min_path = min(min_path, dis[path[i].s]);
             //cout << path[i].s << "'s dis: " << dis[path[i].s] << endl;
             min_index = path[i].s;
         }
     }
     fout <<  min_index << " " << min_path << endl;

     ;
 }