Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UVa Q544
- #include <bits/stdc++.h>
- using namespace std;
- struct stat{
- int V, W;
- bool operator<(const stat &x)const{return W < x.W;}
- };
- vector<stat> v[200];
- bool vis[200];
- int32_t main(){
- int n,m,x, cnt = 0;
- while(cin >> n >> m){
- if(n == 0 && m == 0)break;
- for(int i = 0; i < n; ++i){
- v[i].clear();
- vis[i] = 0;
- }
- string s1, s2;
- map<string, int> cite;
- map<string, int>::iterator it;
- int k = 0;
- for(int i = 0; i < m; ++i){
- cin >> s1 >> s2 >> x;
- it = cite.find(s1);
- if(it == cite.end())cite[s1] = k++;
- it = cite.find(s2);
- if(it == cite.end())cite[s2] = k++;
- stat in = {.V = cite[s2], .W = x};
- v[cite[s1]].emplace_back(in);
- in = {.V = cite[s1], .W = x};
- v[cite[s2]].emplace_back(in);
- }
- cin >> s1 >> s2;
- int start = cite[s1];
- int ended = cite[s2];
- priority_queue<stat> pq;
- stat tmd = {.V=start, .W=1000000000};
- pq.push(tmd);
- int ans = -1;
- while(!pq.empty() && ans == -1){
- tmd = pq.top();
- pq.pop();
- vis[tmd.V] = 1;
- //cout << tmd.V << ' ' << tmd.W<<endl;
- if(tmd.V == ended){
- ans = tmd.W;
- break;
- };
- for(unsigned i = 0; i < v[tmd.V].size(); ++i){
- if(!vis[v[tmd.V][i].V]){
- stat in = v[tmd.V][i];
- in.W = min(in.W, tmd.W);
- pq.push(in);
- }
- }
- }
- cout << "Scenario #" << ++cnt << '\n';
- cout << ans << " tons\n\n";
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement