2019年第二阶段我要变强个人训练赛第十八场 路 (最短路,枚举最短路的边)

 

问题 F: 路

时间限制: 1 Sec  内存限制: 128 MB 

题目描述

Farmer John 热衷于散步,每天早上他都要从 1 号仓库走到 n 号仓库。 Farmer John 家的 n 个仓库被 m 条双向道路连通起来,每条道路有一个长度 w。而Farmer John 又不喜欢走路,所以他走的是从 1 号仓库到 n 号仓库的最短路。
但是 Farmer 的奶牛们总想搞点事情,他们计划着把 m 条道路的其中一条变成原来长度的 2 倍,使得 Farmer John 可能会多走一点路。
他们想知道,最多能让 Farmer John 多走多少路呢?

 

输入

第一行一个正整数 n,m,表示仓库个数和道路条数。
接下来 m 行,每行三个正整数,表示每条双向道路的连接的仓库和该双向道路的长度。

 

输出

输出只有一行,表示最多能让 Farmer John 每天早上多走多少路。

 

样例输入

复制样例数据

5 7
2 1 5
1 3 1
3 2 8
3 5 7
3 4 3
2 4 7
4 5 2

样例输出

2

 

提示

一开始的最短路为1→3→4→5,长度为1+3+2=6。
将连接3和4的边变为原来的两倍,3×2=6。
改造后的图,最短路为1→3→5,长度为1+7=8。
多走了8−6=2的路程,可以证明这是最大的答案。
对于50%的数据,1≤n≤50。
对于100%的数据,1≤n≤250,1≤m≤25000,1≤w≤106。
保证没有重边。

 

思路:由于只有250个点,最短路撑死也就249条边所以  枚举最短路上的边跑做短路取个最大值就行了

(不知道为什么,跑dijistra 用队列还不对,用上优先队列就过了,搞不懂)

#include <bits/stdc++.h> 
using namespace std;
const int maxn = 1e5+6;
const int N = 300;
struct node{
    int to, w, nex, id;
}e[maxn];
bool vis[N];
int cnt, n, m, light;
int pre[N], idd[N], dis[N], head[N];

void add(int x, int y, int w, int id){
    e[cnt].w=w;
    e[cnt].to = y;
    e[cnt].id = id;
    e[cnt].nex = head[x];
    head[x] = cnt++;
}

struct node1 {
    int to, w;
    node1(){}
    node1(int _to, int _w){
        to = _to;
        w = _w;
    }
    bool operator < (const node1 &a) const {
        return w > a.w;
    }
};

void dj(int st, int flag) {
    priority_queue<node1> q;
    q.push(node1(st, 0));
    for(int i = 1; i <= n; i++){
        dis[i] = 1e9;
        vis[i] = false;
    }
    dis[st] = 0;
    while(!q.empty()) {
        node1 fa = q.top(); q.pop();
        int x = fa.to;
        if(vis[x])continue;
        vis[x] = true;
        for(int i = head[x]; ~i ;i = e[i].nex) {
            int to = e[i].to;
            int w = e[i].w;
            if(light == e[i].id) w = w * 2;
            if(dis[to] > dis[x] + w) {
                dis[to] = dis[x] + w;
                if(flag) {
                    pre[to] = x;
                    idd[to] = e[i].id;
                }
                q.push(node1(to, dis[to]));
            }
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i = 1; i <= n; i++) head[i] = -1;
    for(int i = 1; i <= m; i++){
        int x, y, w;
        scanf("%d %d %d", &x, &y, &w);
        add(x, y, w, i);
        add(y, x, w, i);
    }
    dj(1, 1);
    int ans = dis[n];
    int maxx = 0, st = n;
    while(pre[st]) {
        light = idd[st];
        dj(1, 0);
        maxx = max(maxx, dis[n]);
        st = pre[st];
    }
    printf("%d\n", maxx - ans);
    return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值