- #include <bits/stdc++.h>
- #define C 103
- #define MX 1000000000
- using namespace std;
- vector <int> Junction[C], cost[C];
- int dist[C];
- struct data{
- int source, weight;
- data(int x, int y){
- source = x, weight = y;
- }
- bool operator < (const data& p)const{
- weight < p.weight;
- }
- };
- void dijkstra(int n, int src){
- for(int i = 0; i <= n; i++)dist[i] = MX;
- dist[src] = 0;
- priority_queue <data> Q;
- Q.push(data(src, 0));
- while(!Q.empty()){
- data top = Q.top();
- int u = top.source;
- Q.pop();
- int sz = Junction[u].size();
- for(int i = 0; i < sz; i++){
- int v = Junction[u][i];
- if(dist[u]+cost[u][i] < dist[v]){
- dist[v] = dist[u]+cost[u][i];
- Q.push(data(v, dist[v]));
- }
- }
- }
- if(dist[n] == MX)puts("Impossible");
- else printf("%d\n", dist[n]);
- }
- int main(){
- int n, m, t, u, v, w, cs = 0;
- scanf("%d", &t);
- while(t--){
- scanf("%d %d", &n, &m);
- for(int i = 0; i < m; i++){
- scanf("%d %d %d", &u, &v, &w);
- cost[u].push_back(w);
- cost[v].push_back(w);
- Junction[u].push_back(v);
- Junction[v].push_back(u);
- }
- printf("Case %d: ", ++cs);
- dijkstra(n, 1);
- for(int i = 0; i < C; i++)Junction[i].clear(), cost[i].clear();
- }
- return 0;
- }
Saturday, June 6, 2015
LightOJ - 1019 - Brush (V)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment