• POJ 2449 Remmarguts' Date k短路


    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 17094   Accepted: 4694

    Description

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 

    "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 

    "Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 

    Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 

    DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 

    Input

    The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 

    The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

    Output

    A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

    Sample Input

    2 2
    1 2 5
    2 1 4
    1 2 2
    

    Sample Output

    14







    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <cstring>
    
    using namespace std;
    
    const int maxn=1111;
    const int maxm=111111;
    const int INF=1e9;
    
    struct ANODE
    {
        int id;
        int f;
        int g;
        bool operator < (const ANODE &t) const
        {
            if (t.f==f) return t.g<g;
            return t.f<f;
        }
    };
    
    struct EDGE
    {
        int to;
        int w;
        int next;
    }edges[maxm],unti_edges[maxm];
    
    int node,src,dest,edge;
    int head[maxn],dist[maxn];
    
    int unti_head[maxn],unti_edge;
    
    void prepare(int _node,int _src=0,int _dest=0)
    {
        node=_node,src=_src,dest=_dest;
        for (int i=0; i<=node; i++) head[i]=-1;
        edge=0;
        for (int i=0; i<=node; i++) unti_head[i]=-1;
        unti_edge=0;
    }
    
    void addedge(int u,int v,int c)
    {
        edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        //edges[edge].w=c,edges[edge].to=u,edges[edge].next=head[v],head[v]=edge++;
    }
    
    void unti_addedge(int u,int v,int c)
    {
        unti_edges[unti_edge].w=c,unti_edges[unti_edge].to=v,unti_edges[unti_edge].next=unti_head[u],unti_head[u]=unti_edge++;
        //unti_edges[unti_edge].w=c,unti_edges[unti_edge].to=u,unti_edges[unti_edge].next=unti_head[v],unti_head[v]=unti_edge++;
    }
    
    bool spfa(int node,int src,int head[],EDGE edges[],int dist[])
    {
        int i,l,r,u,v,w;
        bool visit[maxn];
        int q[maxn],outque[maxn];
        memset(visit,0,sizeof(visit));
        memset(outque,0,sizeof(outque));
        for (int i=0; i<=node; i++) dist[i]=INF;
        r=0;
        q[r++]=src;
        dist[src]=0;
        visit[src]=true;
        for (l=0; l!=r; ( (++l>=maxn)?(l=0):(1) ))
        {
            u=q[l];
            visit[u]=false;
            outque[u]++;
            if (outque[u]>node) return false;
            for (i=head[u]; i!=-1; i=edges[i].next)
            {
                v=edges[i].to;
                w=edges[i].w;
                if (dist[u]+w<dist[v])
                {
                    dist[v]=dist[u]+w;
                    if (visit[v]) continue;
                    q[r++]=v;
                    visit[v]=true;
                    if (r>=maxn) r=0;
                }
            }
        }
        return true;
    }
    
    int a_star(int start,int end,int n,int k,int head[],EDGE edges[],int dist[])
    {
        priority_queue<ANODE> que;
        ANODE u,v;
        int cnt=0;
        if (start==end) k++;
        if (dist[start]==INF) return -1;
        u.id=start;
        u.g=0;
        u.f=u.g+dist[u.id];
        que.push(u);
        while (!que.empty())
        {
            u=que.top();
            que.pop();
            if (u.id==end) cnt++;
            if (cnt==k) return u.g;
            for (int i=head[u.id]; i!=-1; i=edges[i].next)
            {
                v.id=edges[i].to;
                v.g=u.g+edges[i].w;
                v.f=v.g+dist[v.id];
                que.push(v);
            }
        }
        return -1;
    }
    
    int main()
    {
        int n,m,s,t,k;
        while (~scanf("%d%d",&n,&m))
        {
            prepare(n);
            while (m--)
            {
                int u,v,c;
                scanf("%d%d%d",&u,&v,&c);
                addedge(u,v,c);
                unti_addedge(v,u,c);
            }
            scanf("%d%d%d",&s,&t,&k);
            spfa(node,t,unti_head,unti_edges,dist);
            //spfa(node,t,head,edges,dist);
            int kthlength=a_star(s,t,node,k,head,edges,dist);
            printf("%d\n",kthlength);
        }
        return 0;
    }
    





  • 相关阅读:
    [CQOI2015]选数
    利用匈牙利算法&HopcroftKarp算法解决二分图中的最大二分匹配问题 例poj 1469 COURSES
    玩家死亡,屏幕灰白效果实现
    Bool值相加
    (转)D3D性能优化
    Flash AS3视频教程
    Accurately Profiling Direct3D API Calls (Direct3D 9)
    菲涅尔反射
    sscanf时用到郁闷问题
    如何精确测量程序运行时间(转)
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038429.html
Copyright © 2020-2023  润新知