poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接

两次求最短路(第二次把边反向求)

1、spfa

//poj 3268 Silver Cow Party
//SPFA

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int M = 100000 + 100;
const int N = 1000 + 100;
const int inf = 1<<25;

struct Graph {
    int head[N], next[M], to[M], val[M], cnt, n;

    void init(int num)
    {
        memset(head, -1, sizeof head );
        for(int i=1; i<=n; ++i) d[i] = inf;
        cnt = 0;
        n = num;
        while(!q.empty()) q.pop();
    }

    void addedge(int u, int v, int c)
    {
        next[cnt] = head[u];
        to[cnt] = v;
        val[cnt] = c;
        head[u ] = cnt++;
    }

    int d[N], vis[M];
    queue<int> q;

    void spfa(int s)
    {
        memset(vis, 0, sizeof vis );
        d[s] = 0;
        vis[s] = 1;
        q.push(s);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            for(int i=head[u]; ~i; i = next[i]) {
                int v = to[i];
                if(d[v] > d[u] + val[i]) {
                    d[v] = d[u] + val[i];
                    if(!vis[v]) {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
            vis[u] = 0;
        }
    }
} g1, g2;

int n, m, x;

int main()
{
    while(~scanf("%d%d%d", &n, &m, &x)) {
        g1.init(n);
        g2.init(n);
        int u, v, c;
        for(int i=1; i<=m; ++i){
           scanf("%d%d%d", &u, &v, &c);
           g1.addedge(u, v, c);
           g2.addedge(v, u, c);
        }

        g1.spfa(x); g2.spfa(x);

        int ans = 0;
        for(int i=1; i<=n; ++i)
        {
            if(i != x)
            {
                if(g1.d[i] != inf && g2.d[i] != inf)
                {
                    if(ans < g1.d[i] + g2.d[i])
                        ans = g1.d[i] + g2.d[i];
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

2、dijkstra

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int M = 100000 + 100;
const int N = 1000 + 100;
const int inf = 1<<25;
typedef pair<int, int> P;
int n, m, x;

struct node {
    int v;
    int c;
    node(int vv=0, int cc=0): v(vv), c(cc) {}
    /*
    bool operator < (const node& r) const
    {
        return c > r.c;
    }
    */
};

struct Graph
{
    int head[N], next[M], to[M], val[M], cnt, n;

    void init(int nv)
    {
        memset(head, -1, sizeof head );
        n = nv;
        for(int i=1; i<=n; ++i) d[i] = inf;
        cnt = 0;
        while(!q.empty()) q.pop();
    }

    void addedge(int u, int v, int c)
    {
        next[cnt] = head[u];
        to[cnt] = v;
        val[cnt] = c;
        head[u] = cnt++;
    }

    int d[N];
    priority_queue<P, vector<P>, greater<P> > q;

    void dijkstra(int s)
    {
        d[s] = 0;
        q.push(P(0,s));
        while(!q.empty()){
            P p = q.top();
            q.pop();
            int v = p.second;
            if(d[v]<p.first) continue;
            for(int i=head[v]; ~i; i =next[i])
            {
                int k = to[i];
                if(d[k] > d[v] + val[i])
                {
                    d[k] = d[v] + val[i];
                    q.push(P(d[k], k));
                }
            }
        }
    }

}s1, s2;

int main()
{
    int u, v, c;
    scanf("%d%d%d", &n, &m, &x);
    s1.init(n); s2.init(n);
    for(int i=1; i<=m; ++i)
    {
        scanf("%d%d%d", &u, &v, &c);
        s1.addedge(u, v, c);
        s2.addedge(v, u, c);
    }
    s1.dijkstra(x);
    s2.dijkstra(x);
    int ans = 0;
    for(int i=1; i<=n; ++i)
    {
        if(i==x) continue;
        if(s1.d[i] + s2.d[i] > ans)
            ans = s1.d[i] + s2.d[i];
    }
    printf("%d\n", ans);
    return 0;
}

poj 3268 Silver Cow Party , spfa , dijkstra

时间: 2024-08-02 15:12:45

poj 3268 Silver Cow Party , spfa , dijkstra的相关文章

POJ 3268 Silver Cow Party(SPFA)

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

POJ 3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15460   Accepted: 7004 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

POJ 3268 Silver Cow Party (Dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

poj 3268 Silver Cow Party(dijkstra最短路)

题目链接:http://poj.org/problem?id=3268 题目大意:给你N个农场,在X农场要举办一个party,其它农场需要到X农场去,然后还要回来,问N个农场中距离最远的那个至少为多少?,给出的边为单向边... 思路:用dijkstra最初X农场到其它几个农场的最短距离,然后在把边反向,继续求出X到其它几个农场的最短距离,算出最大的那一个... code: #include<cstdio> #include<iostream> #include<cstring

POJ - 3268 Silver Cow Party(dijkstra技巧)

题目链接:http://poj.org/problem?id=3268 题意:n只奶牛(分别在1-n奶牛舍)分别从各自的奶牛舍出发到X奶牛舍,然后回到自己的奶舍(都以最短路),求出哪一只奶牛花费的距离最远. 题解:n到达1000,想直接Floyd肯定不行. 从X奶牛舍回家,就直接以X为源点最短路就可以了:从各自的奶牛舍去X奶牛舍可以以X为源点反方向进行求最短路. 1 #include <iostream> 2 #include <algorithm> 3 #include <

(简单) POJ 3268 Silver Cow Party,Dijkstra。

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads conn

POJ 3268 Silver Cow Party ( Dijkstra )

题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以X为终点,求出X到其他每个点的距离, 再将图反存一下,在做一次最短路, 两次距离相加求出最长的时间. 这里是用Dijkstra写的,我们第一次用邻接矩阵写,第二次用邻接表,并且有优先队列优化 1 #include <iostream> 2 #include <cmath> 3 #inc

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X