Heavy Transportatio

松弛操作

我们改变松弛操作为d[v]=min(d[u],e.cost)
并维护一个大顶堆。
注意输出格式

代码如下:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
typedef pair<int, int> pii;
const int inf = 2e9;
const int max_n = 1100;
const int max_m = 1e6 + 100;
struct edge{
    int to, cost, next;
}E[max_m << 1];
int head[max_n];
int cnt = 1;
void add(int from, int to, int cost) {
    E[cnt].to = to;E[cnt].cost = cost;
    E[cnt].next = head[from];head[from] = cnt++;
}

int d[max_n];
int dijstra(int s,int t) {
    priority_queue<pii> que;
    fill(d, d + max_n, -inf);
    d[s] = inf;
    que.push(make_pair(d[s], s));
    while (!que.empty()) {
        pii p = que.top();que.pop();
        int u = p.second;int dist = p.first;
        if (dist < d[u])continue;
        for (int i = head[u];i;i = E[i].next) {
            edge& e = E[i];
            if (d[e.to] < min(d[u], e.cost)) {
                d[e.to] = min(d[u], e.cost);
                que.push(make_pair(d[e.to], e.to));
            }
        }
    }return d[t];
}

int N, M;
int main() {
    int tcase;scanf("%d", &tcase);
    for (int t = 1;t <= tcase;++t) {
        fill(head, head + max_n, 0);cnt = 1;
        scanf("%d %d", &N, &M);
        for (int i = 1, u, v, w;i <= M;++i) {
            scanf("%d %d %d", &u, &v, &w);
            add(u, v, w);add(v, u, w);
        }printf("Scenario #%d:\n%d\n\n", t, dijstra(1, N));
    }
}

刷kuangbin大佬的题单,变强。。。。。。

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:00
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
找工作时遇到的神仙HR
点赞 评论 收藏
分享
小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务