码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

时间:2015-07-29 21:23:33      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:poj   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 ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow‘s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

题意:

奶牛去目的地的最短路加上返回的最短路,求所有奶牛必须花费的最大时间。

思路:

最短路的算法都可以,要注意设立两个dist数组,分别记录奶牛去的最短路和返回的最短路。最后求dist1[]+dist2[]最大值即。

代码:

//SPFA,其实代码不多,基本一样

//440K	110MS
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define maxx 1010
#define inf 0x7fffffff
struct Edge
{
    int to,v;
};
vector<Edge> E1[maxx];
vector<Edge> E2[maxx];
queue<int>q;
bool vis[maxx];//是否入队
int dist[maxx];
int dist1[maxx];
int n,m,x;

void spfa()
{
    memset(vis,false,sizeof(vis));
    while(!q.empty())
        q.pop();
    for(int i=1;i<=n;i++)
        dist[i]=inf;
    dist[x]=0;
    q.push(x);
    vis[x]=true;
    while(!q.empty())
    {
        int temp=q.front();
        for(int i=0;i<(int)E1[temp].size();i++)
        {
            Edge *t=&E1[temp][i];
            if(dist[temp]+(*t).v<dist[(*t).to])
            {
                dist[(*t).to]=dist[temp]+(*t).v;
                if(!vis[(*t).to])
                {
                    q.push((*t).to);
                    vis[(*t).to]=true;
                }
            }
        }
        q.pop();
        vis[temp]=false;
    }

    memset(vis,false,sizeof(vis));
    while(!q.empty())
        q.pop();
    for(int i=1;i<=n;i++)
        dist1[i]=inf;
    dist1[x]=0;
    q.push(x);
    vis[x]=true;
    while(!q.empty())
    {
        int temp=q.front();
        for(int i=0;i<(int)E2[temp].size();i++)
        {
            Edge *t=&E2[temp][i];
            if(dist1[temp]+(*t).v<dist1[(*t).to])
            {
                dist1[(*t).to]=dist1[temp]+(*t).v;
                if(!vis[(*t).to])
                {
                    q.push((*t).to);
                    vis[(*t).to]=true;
                //cnt[(*t).to]++;
                }
            }
        }
        q.pop();
        vis[temp]=false;
    }
    int minn=-1;
    for(int i=1;i<=n;i++)
    {
        if(dist[i]+dist1[i]>minn)
            minn=dist[i]+dist1[i];
    }
    cout<<minn<<endl;
}

int main()
{
    cin>>n>>m>>x;
    for(int i=0,a,b,t;i<m;i++)
    {
        cin>>a>>b>>t;
        Edge tmp;
        tmp.to=b;
        tmp.v=t;
        E1[a].push_back(tmp);
        tmp.to=a;
        tmp.v=t;
        E2[b].push_back(tmp);
    }
    spfa();
    return 0;
}

//dijkstra

记的修改一下

#include<iostream>
#include<cstring>
using namespace std;
#define max 1010
#define inf 0x7fffffff
int G[max][max],d[max],db[max],vis[max];
int n,m,x;

void init(int n)
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            G[i][j]=((i==j)?0:inf);
}

int dij()
{
    memset(vis,0,sizeof(vis));
    int min,k;
    for(int i=1;i<=n;i++)
    {
        d[i]=G[x][i];
        db[i]=G[i][x];
    }
    d[x]=0;vis[x]=1;
    db[x]=0;
    for(int i=1;i<n;i++)
    {
        min=inf;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&d[j]<min)
            {
                min=d[j];
                k=j;
            }
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&d[j]>d[k]+G[k][j])
                d[j]=d[k]+G[k][j];
        }
    }
    memset(vis,0,sizeof(vis));
    vis[x]=1;
    for(int i=1;i<n;i++)
    {
        min=inf;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&db[j]<min)
            {
                min=db[j];k=j;
            }
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&db[j]>db[k]+G[k][j])
                db[j]=db[k]+G[k][j];
        }
    }
    int mi=-1;
    for(int i=1;i<=n;i++)
        if(d[i]+db[i]>mi)
          mi=d[i]+db[i];
    return mi;
}

int main()
{
    cin>>n>>m>>x;
    int a,b,c;
    init(n);
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b>>c;
        G[a][b]=c;
    }
    cout<<dij()<<endl;
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

标签:poj   dijkstra   spfa   

原文地址:http://blog.csdn.net/kaisa158/article/details/47133147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!