Algoogle

Algorithm for Programming Contest

PKU 2387 Til the Cows Come Home

Category: PKU Tag: dijkstra

Til the Cows Come Home

問題概要


解法


Dijkstraやるだけ.

コード


(2387.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>

using namespace std;

int T, N;
int way[1024][1024];
vector<int> edge[1024];
bool visited[1024];


int main(){
    scanf("%d%d", &T, &N);

    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            way[i][j] = 1e9;
    for(int i = 0; i < T; i++){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        a--; b--;
        if(way[a][b] == 1e9){
            edge[a].push_back(b);
            edge[b].push_back(a);
        }
        way[a][b] = way[b][a] = min(way[a][b], c);
    }
    priority_queue<pair<int,int>, vector<pair<int,int> >,greater<pair<int,int> > > que;
    que.push(make_pair(0,0));
    while(!que.empty()){
        pair<int,int> p = que.top(); que.pop();
        int from = p.second, dist = p.first;
        if(visited[from]) continue;
        visited[from] = 1;
        if(from == N-1){
            printf("%d\n", dist);
            break;
        }
        for(int i = 0; i < edge[from].size(); i++)
            que.push(make_pair(dist+way[from][edge[from][i]], edge[from][i]));
    }

    return 0;
}

Comments