1450. Russian Pipelines

http://acm.timus.ru/problem.aspx?space=1&num=1450

水题  最短路 spfa

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<string>

#include<vector>

#include<map>

#include<queue>

#include<stack>

#include<cmath>

#define LL long long

//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;



const int INF=0x3f3f3f3f;

const int N=505;

const int M=1000005;

int head[N],I;

struct node

{

    int j,next,v;

}side[M];

void Add(int i,int j,int v)

{

    side[I].j=j;

    side[I].v=v;

    side[I].next=head[i];

    head[i]=I++;

}

int spfa(int s,int f)

{

    int dist[N];

    bool in[N];

    memset(dist,0,sizeof(dist));

    memset(in,false,sizeof(in));

    queue<int>qt;

    qt.push(s);

    in[s]=true;

    while(!qt.empty())

    {

        int x=qt.front();qt.pop();

        in[x]=false;

        for(int t=head[x];t!=-1;t=side[t].next)

        {

            int l=side[t].j;

            if(dist[l]<dist[x]+side[t].v)

            {

                dist[l]=dist[x]+side[t].v;

                if(!in[l])

                {

                    in[l]=true;

                    qt.push(l);

                }

            }

        }

    }

    return dist[f];

}

int main()

{

    //freopen("data.txt","r",stdin);

    int n,m;

    while(scanf("%d %d",&n,&m)!=EOF)

    {

        memset(head,-1,sizeof(head));

        I=0;

        while(m--)

        {

            int a,b,c;

            scanf("%d %d %d",&a,&b,&c);

            Add(a,b,c);

        }

        int S,F;

        scanf("%d %d",&S,&F);

        int ans=spfa(S,F);

        if(ans==0)

        printf("No solution\n");

        else

        printf("%d\n",ans);

    }

    return 0;

}

 

你可能感兴趣的:(pipeline)