其他分享
首页 > 其他分享> > P4568 [JLOI2011]飞行路线

P4568 [JLOI2011]飞行路线

作者:互联网

P4568 [JLOI2011]飞行路线

Description

Input

Output

Sample Input

5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100

Sample Output

8

Data Size

题解:

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

struct Node
{
    int val, pos;
    friend bool operator < (Node x, Node y) {
        return x.val > y.val;
    }
};
struct E {int next, to, dis;} e[M];
int n, m, k, s, t, num, ans = 0x7fffffff;
int h[N], dis[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;
}

void dijskra()
{
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<Node> que;
    dis[s] = 0, que.push((Node){0, s});
    while(!que.empty())
    {
        int x = que.top().pos;  que.pop();
        if(vis[x]) continue;
        vis[x] = 1;
        for(int i = h[x]; i != 0; i = e[i].next)
            if(dis[x] + e[i].dis < dis[e[i].to])
            {
                dis[e[i].to] = dis[x] + e[i].dis;
                if(!vis[e[i].to]) que.push((Node){dis[e[i].to], e[i].to});
            }
    }
}

int main()
{
    cin >> n >> m >> k >> s >> t;
    s++, t++;
    for(int i = 1; i <= m; i++)
    {
        int u = read() + 1, v = read() + 1, 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);
        }
    }
    dijskra();
    for(int i = 0; i <= k; i++)
        ans = min(ans, dis[t + i * n]);
    cout << ans; 
    return 0;
}

标签:Node,航线,JLOI2011,int,P4568,路线,que,城市,dis
来源: https://www.cnblogs.com/BigYellowDog/p/11216971.html