UVALive 6885 Flowery Trails

两次SPFA

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

const int INF=0x7FFFFFFF;
const int maxn=10000+10;
const int Maxn=250000+10;
int N,M;
int U[Maxn],V[Maxn],C[Maxn];
struct edge
{
    int from,to,cost;
}e[Maxn];
vector<int>G[maxn];
int dis1[maxn],flag1[maxn];
int dis2[maxn],flag2[maxn];

void init()
{
    for(int i=0;i<maxn;i++) G[i].clear();
}

void spfa()
{
    queue<int>Q;
    for(int i=0;i<maxn;i++) dis1[i]=INF;
    dis1[0]=0;
    flag1[0]=1;
    Q.push(0);
    while(!Q.empty())
    {
        int h=Q.front();Q.pop(); flag1[h]=0;
        for(int i=0;i<G[h].size();i++)
        {
            int id=G[h][i];
            if(e[id].from==h)
            {
                if(dis1[h]+e[id].cost<dis1[e[id].to])
                {
                    dis1[e[id].to]=dis1[h]+e[id].cost;
                    if(flag1[e[id].to]==0)
                    {
                        flag1[e[id].to]=1;
                        Q.push(e[id].to);
                    }
                }
            }
            else if(e[id].to==h)
            {
                if(dis1[h]+e[id].cost<dis1[e[id].from])
                {
                    dis1[e[id].from]=dis1[h]+e[id].cost;
                    if(flag1[e[id].from]==0)
                    {
                        flag1[e[id].from]=1;
                        Q.push(e[id].from);
                    }
                }
            }
        }
    }
}


void SPFA()
{
    queue<int>Q;
    for(int i=0;i<maxn;i++) dis2[i]=INF;
    dis2[N-1]=0;
    flag2[N-1]=1;
    Q.push(N-1);
    while(!Q.empty())
    {
        int h=Q.front();Q.pop(); flag2[h]=0;
        for(int i=0;i<G[h].size();i++)
        {
            int id=G[h][i];
            if(e[id].from==h)
            {
                if(dis2[h]+e[id].cost<dis2[e[id].to])
                {
                    dis2[e[id].to]=dis2[h]+e[id].cost;
                    if(flag2[e[id].to]==0)
                    {
                        flag2[e[id].to]=1;
                        Q.push(e[id].to);
                    }
                }
            }
            else if(e[id].to==h)
            {
                if(dis2[h]+e[id].cost<dis2[e[id].from])
                {
                    dis2[e[id].from]=dis2[h]+e[id].cost;
                    if(flag2[e[id].from]==0)
                    {
                        flag2[e[id].from]=1;
                        Q.push(e[id].from);
                    }
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&N,&M)){
    init();
    for(int i=1;i<=M;i++)
    {
        scanf("%d%d%d",&U[i],&V[i],&C[i]);
        e[i].from=U[i];
        e[i].to=V[i];
        e[i].cost=C[i];
        G[U[i]].push_back(i);
        G[V[i]].push_back(i);
    }
    spfa();
    SPFA();

    int ans=0;
    int Len=dis1[N-1];
    for(int i=1;i<=M;i++)
    {
        if(dis1[U[i]]+dis2[V[i]]+C[i]==Len||dis2[U[i]]+dis1[V[i]]+C[i]==Len)
            ans=ans+C[i]+C[i];
    }
    printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2015-09-29 18:53  Fighting_Heart  阅读(204)  评论(0编辑  收藏  举报