D - Silver Cow Party 最短路

http://poj.org/problem?id=3268

题目思路:

直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 2e5 + 10;
int d[maxn], dis[maxn], n, m;
struct node
{
    int from, to, dist;
    node(int from=0,int to=0,int dist=0):from(from),to(to),dist(dist){}
};

struct heapnode
{
    int u, d;
    heapnode(int u=0,int d=0):u(u),d(d){}
    bool operator<(const heapnode&a)const
    {
        return a.d < d;
    }
};
vector<node>vec[maxn];
bool vis[maxn];
void dij(int s)
{
    priority_queue<heapnode>que;
    for (int i = 0; i <= n; i++) d[i] = inf;
    d[s] = 0;
    memset(vis, 0, sizeof(vis));
    que.push(heapnode(s, 0));
    while(!que.empty())
    {
        heapnode x = que.top(); que.pop();
        int u = x.u;
        if (vis[u]) continue;
        vis[u] = 1;
        for(int i=0;i<vec[u].size();i++)
        {
            node e = vec[u][i];
            if(d[e.to]>d[u]+e.dist)
            {
                d[e.to] = d[u] + e.dist;
                que.push(heapnode(e.to, d[e.to]));
            }
        }
    }
}

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    for(int i=1;i<=m;i++)
    {
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        vec[x].push_back(node(x, y, c));
    }
    int ans = 0;
    dij(k);
    for (int i = 0; i <= n; i++) dis[i] = d[i];
    for(int i=1;i<=n;i++)
    {
        if (i == k) continue;
        dij(i);
    //    printf("dis[%d]=%d d[%d]=%d\n", i, dis[i], k, d[k]);
        ans = max(ans, dis[i] + d[k]);
    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/EchoZQN/p/10873477.html