zoukankan      html  css  js  c++  java
  • poj 2449 k短路--模板

    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 17853   Accepted: 4879

    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

    Source

    POJ Monthly,Zeyuan Zhu
    -------------------------
    很久以前写的。。
    #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
    ",kthlength);
        }
        return 0;
    }
    


  • 相关阅读:
    jmeter如何引用自己编写的java文件编译的jar包
    Vue+Django REST framework 打造生鲜电商项目(学习笔记二)
    mysql笔试题
    面试遇到的问题
    Idea中maven项目pom文件中已引入testng但项目文件中无法引入@Test
    记录一次TestNg+MyBatis中的SqlSession出现的问题,问题虽然解决了但尚未明白问题原因
    PyMySQL的基本操作
    MySQL循环语句
    Vue父子组件和非父子组件间的通信
    Python的静态方法和类成员方法
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226273.html
Copyright © 2011-2022 走看看