POJ--3268--Silver Cow Party【SPFA+邻接表】

题意:一些牛要去某一点参加聚会,然后再回到自己家,路是单向的,问花费时间最多的那头牛最少需要花费多长时间。

思路:从聚会地点返回,相当于是从某一点到其他各个点的最短路径。从牛的家中走到聚会地点,可以把路径反过来变成从聚会地点到各个点的最短路径,两个最短路径值加起来就是每头牛所花费的最小时间,找出最大的即可。

我用了两个邻接表存路径,其实这道题用邻接矩阵存更好做,矩阵横纵坐标翻转就把路径反转了,我用SPFA写想练练手,一直都不会手写SPFA,做几道题找找感觉。

AC居然用时0MS。。

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 100100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct node{
    int u,v,next;
}edge[MAXN],redge[MAXN];
int head[MAXN],rhead[MAXN],dist[MAXN],vis[MAXN],ans[MAXN];
int cnt,rcnt,n,m,x;
void add_edge(int a,int b,int c){
    edge[cnt].u = b;
    edge[cnt].v = c;
    edge[cnt].next = head[a];
    head[a] = cnt++;
    redge[rcnt].u = a;
    redge[rcnt].v = c;
    redge[rcnt].next = rhead[b];
    rhead[b] = rcnt++;
}
void spfa(int type){
    int i,j;
    for(i=1;i<=n;i++){
        dist[i] = INF;
    }
    dist[x] = 0;
    memset(vis,0,sizeof(vis));
    vis[x] = 1;
    queue<int>q;
    q.push(x);
    while(!q.empty()){
        int temp = q.front();
        q.pop();
        vis[temp] = 0;
        for(i=type?rhead[temp]:head[temp];i!=-1;i=type?redge[i].next:edge[i].next){
            int lu = type?redge[i].v:edge[i].v;
            if(lu+dist[temp]<dist[type?redge[i].u:edge[i].u]){
                dist[type?redge[i].u:edge[i].u] = lu + dist[temp];
                if(!vis[type?redge[i].u:edge[i].u]){
                    vis[type?redge[i].u:edge[i].u] = 1;
                    q.push(type?redge[i].u:edge[i].u);
                }
            }
        }
    }
}
int main(){
    int i,j,a,b,c;
    scanf("%d%d%d",&n,&m,&x);
    memset(head,-1,sizeof(head));
    memset(rhead,-1,sizeof(rhead));
    cnt = rcnt = 0;
    int maxm = 0;
    for(i=0;i<m;i++){
        scanf("%d%d%d",&a,&b,&c);
        add_edge(a,b,c);
    }
    spfa(0);
    for(i=1;i<=n;i++){
        if(i!=x)    ans[i] = dist[i];
    }
    spfa(1);
    for(i=1;i<=n;i++){
        if(i!=x)    ans[i] += dist[i];
        if(ans[i]>maxm) maxm = ans[i];
    }
    printf("%d\n",maxm);
    return 0;
}

POJ--3268--Silver Cow Party【SPFA+邻接表】,布布扣,bubuko.com

时间: 2024-10-03 23:53:20

POJ--3268--Silver Cow Party【SPFA+邻接表】的相关文章

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 , 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

图论 ---- 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 

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 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||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 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)

题目链接:POJ 3268 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

POJ 3268 Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20274   Accepted: 9278 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