通往奥格瑞玛的道路_洛谷1462_二分+spfa

来源:互联网 发布:中企动力大数据推广 编辑:程序博客网 时间:2024/06/06 03:07

题目背景


在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量

有一天他醒来后发现自己居然到了联盟的主城暴风城

在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛

题目描述


在艾泽拉斯,有n个城市。编号为1,2,3,…,n。

城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量。

没经过一个城市,都会被收取一定的过路费(包括起点和终点)。路上并没有收费站。

假设1为暴风城,n为奥格瑞玛,而他的血量最多为b,出发时他的血量是满的。

歪嘴哦不希望花很多钱,他想知道,在可以到达奥格瑞玛的情况下,他所经过的所有城市中最多的一次收取的费用的最小值是多少。

输入格式:


第一行3个正整数,n,m,b。分别表示有n个城市,m条公路,歪嘴哦的血量为b。

接下来有n行,每行1个正整数,fi。表示经过城市i,需要交费fi元。

再接下来有m行,每行3个正整数,ai,bi,ci(1<=ai,bi<=n)。表示城市ai和城市bi之间有一条公路,如果从城市ai到城市bi,或者从城市bi到城市ai,会损失ci的血量。

输出格式:


仅一个整数,表示歪嘴哦交费最多的一次的最小值。

如果他无法到达奥格瑞玛,输出AFK。

说明


对于60%的数据,满足n≤200,m≤10000,b≤200

对于100%的数据,满足n≤10000,m≤50000,b≤1000000000

对于100%的数据,满足ci≤1000000000,fi≤1000000000,可能有两条边连接着相同的城市。

题解


看到最大值最小最小值最大就要想到二分答案了
思路就很明确了,二分一下交费最多作为限制跑spfa,看到是否能达终点
一开始写的是二分c的值,然后其实可以二分c的下标,会快很多

Code


#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#define debug puts("-----")#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)#define fill(x, t) memset(x, t, sizeof(x))#define min(x, y) x<y?x:y#define max(x, y) x>y?x:y#define PI (acos(-1.0))#define EPS (1e-8)#define INF (1<<30)#define ll long long#define db double#define ld long double#define N 10001#define E N * 16 + 1#define L 255using namespace std;struct edge{int x, y, w, next;}e[E];bool inQueue[N];int cost[N], dis[N], ls[N], c[N];int edgeCnt;inline int read(){    int x = 0, v = 1;    char ch = getchar();    while (ch < '0' || ch > '9'){        if (ch == '-'){            v = -1;        }        ch = getchar();    }    while (ch <= '9' && ch >= '0'){        x = (x << 1) + (x << 3) + ch - '0';        ch = getchar();    }    return x * v;}inline int addEdge(int &cnt, const int &x, const int &y, int w = 0){    e[++cnt] = (edge){x, y, w, ls[x]}; ls[x] = cnt;    return 0;}inline int spfa(const int &st, const int &ed, const int &res){    queue<int>q;    q.push(st);    fill(inQueue, false);    inQueue[st] = true;    fill(dis, 63);    dis[st] = 0;    while (!q.empty()){        int now = q.front(); q.pop();        for (int i = ls[now]; i; i = e[i].next){            if (dis[now] + e[i].w < dis[e[i].y] && cost[e[i].y] <= res){                dis[e[i].y] = dis[now] + e[i].w;                if (!inQueue[e[i].y]){                    inQueue[e[i].y] = true;                    q.push(e[i].y);                }            }        }        inQueue[now] = false;    }    return dis[ed];}int main(void){    int n = read(), m = read(), lim = read();    rep(i, 1, n){        c[i] = cost[i] = read();    }    sort(c + 1, c + n + 1);    edgeCnt = 1;    rep(i, 1, m){        int x = read(), y = read(), w = read();        addEdge(edgeCnt, x, y, w);        addEdge(edgeCnt, y, x, w);    }    int ans = INF;    for (int l = 0, r = n;l <= r;){        int mid = (l + r) >> 1;        // printf("mid = %d l = %d r = %d\n", mid, l, r);        int hp = spfa(1, n, c[mid]);        if (hp <= lim){            ans = mid;            r = mid - 1;        }else if (hp > lim){            l = mid + 1;        }    }    if (ans == INF){        printf("AFK\n");    }else{        printf("%d\n", c[ans]);    }    return 0;}
0 0
原创粉丝点击