poj3268 Silver Cow Party (SPFA求最短路)

其实还是从一个x点出发到所有点的最短路问题。来和回只需分别处理一下逆图和原图,两次SPFA就行了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-8;
const int INF=1000000000;
const int maxn=1000+10;
int n,m,x,a,b,t;
vector<int>vv1[maxn];
vector<int>vv2[maxn];
int t1[maxn][maxn],t2[maxn][maxn];
int d1[maxn],d2[maxn],ans=0;
//以上1是原图的量,2是逆图的量
queue<int>q;
void spfa(int type)
{
    if(type==1)
    {
        q.push(x);
        while(!q.empty())
        {
            int tt=q.front();
            q.pop();
            int st=vv1[tt].size();
            for(int i=0;i<st;i++)
            {
                int ti=vv1[tt][i];
                if(d1[tt]+t1[tt][ti]<d1[ti])
                {
                    d1[ti]=d1[tt]+t1[tt][ti];
                    q.push(ti);
                }
            }
        }
    }
    else
    {
        q.push(x);
        while(!q.empty())
        {
            int tt=q.front();
            q.pop();
            int st=vv2[tt].size();
            for(int i=0;i<st;i++)
            {
                int ti=vv2[tt][i];
                if(d2[tt]+t2[tt][ti]<d2[ti])
                {
                    d2[ti]=d2[tt]+t2[tt][ti];
                    q.push(ti);
                }
            }
        }
    }
}
int main()
{
    //freopen("in8.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d%d%d",&n,&m,&x);
    fill(d1,d1+maxn,INF);
    fill(d2,d2+maxn,INF);
    d1[x]=d2[x]=0;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&t);
        t1[a][b]=t2[b][a]=t;
        vv1[a].push_back(b);
        vv2[b].push_back(a);
    }
    spfa(1);
    spfa(2);
    for(int i=1;i<=n;i++)
    {
        ans=max(ans,d1[i]+d2[i]);
    }
    printf("%d\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
时间: 2024-10-22 19:25:25

poj3268 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

POJ3268 Silver Cow Party —— 最短路

题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24527   Accepted: 11164 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co

Silver Cow Party--poj3268(SPFA)

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

POJ3268 Silver Cow Party 【Dijkstra】

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

spfa求次短路

思路:先算出每个点到1的最短路d1[i],记录下路径,然后枚举最短路上的边 删掉之后再求一遍最短路,那么这时的最短路就可能是答案. 既然这样为甚么不用A*求次短路呢?因为A*求次短路处理不了无向图啊,他会来回的走.ε=(′ο`*)))唉 上菜: 集合位置 题目描述 每次有大的活动,大家都要在一起"聚一聚",不管是去好乐迪,还是避风塘,或者汤姆熊,大家都要玩的痛快.还记得心语和花儿在跳舞机上的激情与释放,还记得草草的投篮技艺是如此的高超,还记得狗狗的枪法永远是'S'--还有不能忘了,胖子

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 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( 最短路 )

题目链接:http://poj.org/problem?id=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 road

poj3268——Silver Cow Party(最短路+godv之力)

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