The road leading to Orgrimmar - half the shortest +

The road to Orgrimmar

topic:

In Azeroth, there are n cities. Numbered 1,2,3, ..., n.

Between city m two-way road, connecting the two cities, from one city to another, it will attack the league, and then a certain amount of blood loss.

Each time through a city, will be charged a certain amount of tolls (including start and end). Road and no toll booths.

Assumption 1 Stormwind, n is Orgrimmar, and his blood up to b, starting when he was full of blood.

Crooked mouth oh do not want to spend a lot of money, he wanted to know, in the case can go to 达奥格瑞玛, minimum, all the cities he passes most of the time in the fees charged by how much.

Input format:
The first line of three positive integer, n, m, b. Represent cities there are n, m road, blood is twisted oh b.

Then there are n lines, each line a positive integer, fi. Expressed through the city i, need to pay fi yuan.

The next m rows, each row of three positive integers, ai, bi, ci (1 <= ai, bi <= n). Expressed a highway between the cities and urban ai bi, ai if from city to city bi, bi or from city to city ai, ci lose blood.

Output format:
single integer representing twisted Oh minimum charge of most of the time.

If he is unable to 达奥格瑞玛 output AFK.

Ideas:

Written questions face of ambiguity. . After all the cities in the / most of the time to charge fees / minimum value, rather than through the minimum of all the city most of the time / cost of charge. . . .
For the maximum in the minimum requirements of this title, usually half, the maximum cost path to guess, based on the maximum use to find the shortest path, until you find the minimum.

Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int n, m, hp;
int f[10005];
int dis[10005],vis[10005];
struct edge
{
    int from, to, dis;
    edge(int a, int b, int c) : from(a), to(b), dis(c){};
};
vector<edge> e;
vector<int> p[10005];
void addedge(int a,int b,int c)
{
    e.push_back(edge(a, b, c));
    int key = e.size();
    p[a].push_back(key-1);
    e.push_back(edge(b, a, c));
    p[b].push_back(key);
}
struct heap
{
    int p, val;
    bool operator<(const heap &a) const
    {
        return val > a.val;
    }
};
priority_queue<heap> q;
bool dijstra(int maxm)
{
    if(f[1]>maxm)
        return false;
    memset(dis, INF, sizeof dis);
    dis[1] = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 2; i <= n;i++)
    {
        if(f[i]>maxm)
            vis[i] = 2;
    }
    q.push(heap{1, 0});
    while (!q.empty())
    {
        heap t = q.top();
        q.pop();
        int x = t.p;
        if (vis[x]!=0)
            continue;
        vis[x] = 1;
        for (int i = 0; i < p[x].size(); i++)
        {
            edge key = e[p[x][i]];
            if (vis[key.to]!=2&&dis[key.to] > dis[x] + key.dis)
            {
                dis[key.to] = dis[x] + key.dis;
                q.push(heap{key.to, dis[key.to]});
            }
        }
    }
    if (dis[n] > hp)
        return false;
    else
        return true;
}
int main()
{
    scanf("%d%d%d", &n, &m, &hp);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &f[i]);
    }
    for (int i = 0; i < m;i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if(a==b)
            continue;
        addedge(a, b, c);
    }
    if(!dijstra(INF))
    {
        printf("AFK\n");
        return 0;
    }
    int low = 0, high = 1e9;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if(dijstra(mid))
            high = mid-1;
        else
            low = mid+1; 
    }
    printf("%d\n", low);
    return 0;
}

Guess you like

Origin www.cnblogs.com/booiris/p/11119957.html