其他分享
首页 > 其他分享> > 洛谷 P2939 [USACO09FEB]改造路Revamping Trails

洛谷 P2939 [USACO09FEB]改造路Revamping Trails

作者:互联网

洛谷 P2939 [USACO09FEB]改造路Revamping Trails

Description

Input

Output

Data Size

题解:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define N 10005 * 42
#define M 100005 * 42
using namespace std;

struct Node
{
    int val, pos;
    friend bool operator < (Node x, Node y) {
        return x.val > y.val;
    }
};
priority_queue<Node> que;
struct E {int next, to, dis;} e[M];
int n, m, k, num, ans = 0x7fffffff;
int dis[N], h[N];
bool vis[N];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

void add(int u, int v, int w)
{
    e[++num].next = h[u];
    e[num].to = v;
    e[num].dis = w;
    h[u] = num;
}

int main()
{
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++)
    {
        int u = read(), v = read(), w = read();
        add(u, v, w), add(v, u, w);
        for(int j = 1; j <= k; j++)
        {
            add(u + j * n, v + j * n, w);
            add(v + j * n, u + j * n, w);
            add(u + (j - 1) * n, v + j * n, 0);
            add(v + (j - 1) * n, u + j * n, 0);
        }
    }
    memset(dis, 0x3f, sizeof(dis));
    dis[1] = 0, que.push((Node){0, 1});
    while(que.size())
    {
        int now = que.top().pos;
        que.pop();
        if(vis[now]) continue;
        vis[now] = 1;
        for(int i = h[now]; i != 0; i = e[i].next)
            if(dis[now] + e[i].dis < dis[e[i].to])
            {
                dis[e[i].to] = dis[now] + e[i].dis;
                que.push((Node){dis[e[i].to], e[i].to});
            }
    }
    for(int i = 0; i <= k; i++) ans = min(ans, dis[n + i * n]);
    cout << ans;
    return 0;
}

标签:洛谷,val,int,小径,Revamping,牧场,USACO09FEB,Line,include
来源: https://www.cnblogs.com/BigYellowDog/p/11515487.html