洛谷 P4822 [BJWC2012]冻结

洛谷 P4822 [BJWC2012]冻结

题目:

  • 分层图模版啊,我实在是太懒了,题目都懒得粘了(反正跟模版几乎一模一样
  • 这题怎么是紫题..不应该绿吗
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define N 55 * 52
#define M 2005 * 52
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, w / 2);
            add(v + (j - 1) * n, u + j * n, w / 2);
        }
    }
    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;
}

转载于:https://www.cnblogs.com/BigYellowDog/p/11515551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值