其他分享
首页 > 其他分享> > 【题解】「JOI 2015 Final」JOI 公园

【题解】「JOI 2015 Final」JOI 公园

作者:互联网

Description

Solution

Code

#include <cstdio>
#include <algorithm>
#include <queue>
#define int long long
using namespace std;
const int MAXN = 1e5 + 10, MAXM = 4e5 + 5, INF = 1e18;
int n, m, C;
int head[MAXN], nxt[MAXM], ver[MAXM], edge[MAXM], tot;
int d[MAXN];
bool vis[MAXN];
void Dijkstra() {
    priority_queue<pair<int, int> > q;
    for (int i = 1; i <= n; i++)
    	d[i] = INF;
    d[1] = 0;
    q.push({0, 1});
    while (q.size()) {
        int u = q.top().second;
        q.pop();
        if (vis[u])
            continue;
        vis[u] = 1;
        for (int i = head[u]; i; i = nxt[i]) {
        	int v = ver[i], w = edge[i];
        	if (d[v] > d[u] + w) {
                d[v] = d[u] + w;
                q.push({-d[v], v});
            }
		}
    }
}
struct Node {
    int w, d;
    Node() {}
    Node(int w, int d): w(w), d(d) {}
    bool operator < (const Node o) const {
        return d < o.d;
    }
} t[MAXM];
int a[MAXM], b[MAXM], c[MAXM];
int sum[MAXN];
int ans;
void add(int x, int y, int w) {
    nxt[++tot] = head[x], head[x] = tot, ver[tot] = y, edge[tot] = w;
}
signed main() {
    scanf("%lld %lld %lld", &n, &m, &C);
    for (int i = 1; i <= m; i++) {
        scanf("%lld %lld %lld", &a[i], &b[i], &c[i]);
        int u = a[i], v = b[i], w = c[i];
        add(u, v, w), add(v, u, w);
        ans += w;
    }
    Dijkstra();
    for (int i = 1; i <= m; i++)
        t[i] = Node(c[i], max(d[a[i]], d[b[i]]));
    sort(t + 1, t + 1 + m);
    for (int i = 1; i <= m; i++)
        sum[i] = sum[i - 1] + t[i].w;
    for (int i = 1; i <= n; i++) {
        int l = 1, r = m, p;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (t[mid].d <= d[i])
                p = mid, l = mid + 1;
            else
                r = mid - 1;
        }
        ans = min(ans, C * d[i] + sum[m] - sum[p]);
    }
	printf("%lld", ans);
    return 0;
}

标签:Node,int,题解,tot,MAXN,MAXM,2015,JOI,lld
来源: https://www.cnblogs.com/zhouziyi/p/16558548.html