Dijkstra(机场快线,UVA 11374)

来源:互联网 发布:盗版qq软件下载 编辑:程序博客网 时间:2024/04/25 20:52
#include<bits/stdc++.h>using namespace std;const int maxn = 1010;const int inf = 0x3f3f3f3f;struct Edge{    int from,to,dist;};struct HeapNode{    int d,u;    bool operator < (const HeapNode& rhs) const    {        return d>rhs.d;    }};struct Dijkstra{    int n,m;    vector<Edge>edges;    vector<int>G[maxn];    int done[maxn];    int d[maxn];    int p[maxn];    int pp[maxn];    void init(int n)    {        this->n=n;        for(int i=0;i<2*n;i++)            G[i].clear();        edges.clear();    }    void add(int u,int v,int d,bool st)    {        u--;v--;        if(st)        {            edges.push_back((Edge){u,v+n,d});            edges.push_back((Edge){v,u+n,d});            int m=edges.size();            G[u].push_back(m-2);            G[v].push_back(m-1);        }        else        {            edges.push_back((Edge){u,v,d});            edges.push_back((Edge){v,u,d});            int m=edges.size();            G[u].push_back(m-2);            G[v].push_back(m-1);            edges.push_back((Edge){u+n,v+n,d});            edges.push_back((Edge){v+n,u+n,d});            m=edges.size();            G[u+n].push_back(m-2);            G[v+n].push_back(m-1);        }    }    void dijkstr(int s)    {        memset(done,0,sizeof(done));        memset(d,inf,sizeof(d));        d[s]=0;        priority_queue<HeapNode>Q;        Q.push((HeapNode){0,s});        while(!Q.empty())        {            HeapNode now=Q.top();            Q.pop();            if(done[now.u]) continue;            done[now.u]=1;            for(unsigned int i=0;i<G[now.u].size();i++)            {                Edge& e=edges[G[now.u][i]];                if(d[e.to]>d[e.from]+e.dist)                {                    d[e.to]=d[e.from]+e.dist;                    p[e.to]=e.from<n&&e.to>=n?e.from:p[e.from];                    pp[e.to]=G[e.from][i];                    Q.push((HeapNode){d[e.to],e.to});                }            }        }    }    void print(int S,int T)    {        if(S==T)        {            printf("%d\n",T+1);            return;        }        stack<int>s;        s.push(T);        int now=T;        while(1)        {            Edge&e=edges[pp[now]];            s.push(e.from);            if(e.from==S) break;            now=e.from;        }        while(!s.empty())        {            printf("%d%c",(s.top()>=n?s.top()-n:s.top())+1,s.size()==1?'\n':' ');            s.pop();        }    }};int N,M,S,E,U,V,W;Dijkstra DIJ;int bfir;int main(){    //freopen("data.txt","r",stdin);    //freopen("wrong.txt","w",stdout);    while(scanf("%d %d %d",&N,&S,&E)==3&&N)    {        if(bfir) puts("");        bfir=true;        DIJ.init(N);        scanf("%d",&M);        while(M--)        {            scanf("%d %d %d",&U,&V,&W);            DIJ.add(U,V,W,0);        }        scanf("%d",&M);        while(M--)        {            scanf("%d %d %d",&U,&V,&W);            DIJ.add(U,V,W,1);        }        S--;        E--;        DIJ.dijkstr(S);        if(DIJ.d[E]<=DIJ.d[E+N])        {            DIJ.print(S,E);            puts("Ticket Not Used");            printf("%d\n",DIJ.d[E]);        }        else        {            DIJ.print(S,E+N);            printf("%d\n%d\n",DIJ.p[E+N]+1,DIJ.d[E+N]);        }    }    return 0;}

0 0
原创粉丝点击