Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- #include <queue>
- #include <functional>
- using namespace std;
- typedef vector<string> vs;
- typedef tuple<int, string, char> path;
- map<string, map<string, vs> > G;
- map<string, int> D;
- string s, e;
- int n, m;
- string dijkstra()
- {
- D.clear();
- priority_queue<path, vector<path>, greater<path> > PQ;
- PQ.emplace(0, s, '0');
- D[s] = 0;
- while (!PQ.empty())
- {
- string u;
- char c;
- int d;
- tie(d, u, c) = PQ.top(); PQ.pop();
- if (u == e)
- return to_string(d);
- if (D[s] && D[s] < d)
- continue;
- for (auto it : G[u])
- {
- string v = it.first;
- for (string a : it.second)
- if (D.find(v) == D.end() || (a[0] != c && D[v] > D[u] + a.size()))
- D[v] = d + a.size(), PQ.emplace(d + a.size(), v, a[0]);
- }
- }
- return "impossivel";
- }
- int main()
- {
- while (cin >> m, m)
- {
- string a, b, d;
- cin >> s >> e;
- for (int i = 0; i < m; ++i)
- {
- cin >> a >> b >> d;
- G[a][b].push_back(d);
- G[b][a].push_back(d);
- }
- cout << dijkstra() << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement