AOJ0616 JOI Park

JOI Park | Aizu Online Judge

Introduction to Programming Introduction to Algorithms and Data Structures Library of Data Structures Library of Graph Algorithms Library of Computational Geometry Library of Dynamic Programming Library of Number Theory

まず,からの各広場までの最短経路を求める.次に,コストが小さい順にソートし,その場所への到達コストを仮のと置き,整備する場所とする.整備される場所同士は,地下道を通して行き来できるためそのコストを全体から引く.始点を到達コストの広場と見れば整備しない場合も考慮される.

Code

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1LL<<60
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct edge {
  int from,to;
  int cost;

  edge(int t,int c) : to(t),cost(c) {}
  edge(int f,int t,int c) : from(f),to(t),cost(c) {}

  bool operator<(const edge &e) const {
      return cost < e.cost;
  }
};

vector<edge> G[100005];
ll d[100005];
bool used[100005];

void dijkstra(int s,int n) {
  priority_queue<P,vector<P>,greater<P> > que;
  fill(d,d+n,INF);

  d[s] = 0;
  que.push(P(0,s));

  while(que.size()) {
      P p = que.top();
      que.pop();

      int v = p.second;
      if(d[v] < p.first) continue;

      rep(i,G[v].size()) {
          edge e = G[v][i];
          if(d[e.to] > d[v] + e.cost) {
              d[e.to] = d[v] + e.cost;
              que.push(P(d[e.to],e.to));
          }
      }
  }
}

int main() {
  int n, m, c;
  cin >> n >> m >> c;

  ll sum = 0;
  rep(i, m) {
      int a, b, d;
      cin >> a >> b >> d;

      a--;
      b--;
      sum += d;

      G[a].push_back(edge(b, d));
      G[b].push_back(edge(a, d));
  }

  dijkstra(0, n);

  vector<pair<ll, int> > v(n);
  rep(i, n) {
      v[i].first = d[i];
      v[i].second = i;
  }

  sort(v.begin(), v.end());
  memset(used, 0, sizeof(used));

  ll ans = INF;
  rep(i, v.size()) {
      int j = v[i].second;
      used[j] = true;

      rep(k, G[j].size()) {
          if(used[G[j][k].to]) {
              sum -= G[j][k].cost;
          }
      }

      ans = min(ans, d[j] * c + sum);
  }

  cout << ans << endl;

  return 0;
}
Mar 18th, 2016