• poj2449第K小路径问题


    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 30017   Accepted: 8159

    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


    A* + 最短路
    http://blog.csdn.net/b2b160/article/details/4057781 贴个网站 A*算法详解
    估价函数 = 当前值+当前位置到终点的距离

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int N=1008 ;
    const int M=100008 ;
    const int INF=0x3f3f3f3f;
    int tot,head[N],re[N],d[N],st,ed,k;
    bool vis[N];
    struct nnn{
       int to,next,w;
    }e[M],e1[M];
    void add(int u,int v,int w){
       e[tot].to=v;
       e[tot].next=head[u];
       e[tot].w=w;
       head[u]=tot;
       e1[tot].to=u;
       e1[tot].next=re[v];
       e1[tot].w=w;
       re[v]=tot++;
    }
    struct node{
        int f,u,g;
        bool operator <(const node &A)const{
          return A.f<f;
        }
    };
    void pre(){
        d[ed]=0;
        memset(vis,0,sizeof(vis));
        memset(d,INF,sizeof(d));
        queue<int>Q;
        Q.push(ed);
        d[ed]=0;
        while(!Q.empty()){
            int u=Q.front();
            Q.pop();
            vis[u]=0;
            for(int i=re[u];i+1;i=e1[i].next){
                int v=e1[i].to;
                if(d[v]>d[u]+e1[i].w){
                    d[v]=d[u]+e1[i].w;
                    if(!vis[v]) {
                        Q.push(v);
                        vis[v]=1;
                    }
                }
            }
        }
    }
    int slove(){
        int cont=0;
        memset(vis,0,sizeof(vis));
        priority_queue<node>Q;
        node p,q;
        p.f=d[st],p.u=st,p.g=0;
        Q.push(p);
        if(p.f==INF) return -1;
        while(!Q.empty()){
            p=Q.top();
            Q.pop();
            if(p.u==ed){
                ++cont;
                if(cont==k) return p.g;
            }
            for(int i=head[p.u];i+1;i=e[i].next){
                int v=e[i].to;
                q.u=v;q.g=p.g+e[i].w;q.f=q.g+d[v];
                Q.push(q);
            }
        }
        return -1;
    }
    int main(){
        int n,m,u,v,w;
        while(scanf("%d%d",&n,&m)!=EOF){
            tot=0;
            memset(head,-1,sizeof(head));
            memset(re,-1,sizeof(re));
            while(m--){
                scanf("%d%d%d",&u,&v,&w);
                add(u,v,w);
            }
            scanf("%d%d%d",&st,&ed,&k);
            pre();
            if(st==ed) ++k;
            printf("%d
    ",slove());
        }
    }
  • 相关阅读:
    HDU 1203 I NEED A OFFER!(0-1背包)
    poj3624 Charm Bracelet(0-1背包 滚动数组)
    Contest1803
    2019 Multi-University Training Contest 3
    Circle and Points
    分治算法应用-最近点对的最小距离-hdu 1007 Quoit Design
    2019年第二阶段我要变强个人训练赛第十七场
    2019牛客暑期多校训练营(第四场)
    2019牛客暑期多校训练营(第三场)
    2019 Multi-University Training Contest 2
  • 原文地址:https://www.cnblogs.com/mfys/p/7232812.html
Copyright © 2020-2023  润新知