Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define inf 1061109567
- #define inf_m 63
- #define mx 5005
- struct node
- {
- int u, cost;
- node(){;}
- node(int _u, int _cost)
- {
- u = _u;
- cost = _cost;
- }
- };
- bool operator < (node A, node B)
- {
- return B.cost < A.cost;
- }
- vector<node>adj[mx];
- int dis_f[mx], dis_s[mx];
- void dijkstra(int s, int e)
- {
- memset(dis_f, 63, sizeof(dis_f));
- memset(dis_s, 63, sizeof(dis_s));
- priority_queue<node>pq;
- dis_f[s] = 0;
- pq.push(node(s, 0));
- node top, temp;
- while(!pq.empty())
- {
- top = pq.top();
- pq.pop();
- if(top.cost > dis_s[top.u])
- continue;
- for(auto it : adj[top.u])
- {
- temp = it;
- if(dis_f[it.u] > top.cost + it.cost)
- {
- dis_s[it.u] = dis_f[it.u];
- dis_f[it.u] = top.cost + it.cost;
- pq.push(node(it.u, dis_f[it.u]));
- }
- else if(top.cost + it.cost > dis_f[it.u] && top.cost + it.cost < dis_s[it.u])
- {
- dis_s[it.u] = top.cost + it.cost;
- pq.push(node(it.u, dis_s[it.u]));
- }
- }
- }
- }
- int main()
- {
- int n, r, t, i, j, u, v, c;
- scanf("%d", &t);
- for(i = 1; i <= t; i++)
- {
- scanf("%d %d", &n, &r);
- while(r--)
- {
- scanf("%d %d %d", &u, &v, &c);
- adj[u].push_back(node(v, c));
- adj[v].push_back(node(u, c));
- }
- dijkstra(1, n);
- printf("Case %d: %d\n", i, dis_s[n]);
- while(n)
- adj[n--].clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement