zoukankan      html  css  js  c++  java
  • poj2449

    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 27699   Accepted: 7500

    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

    题解:

    k短路模板

    AC代码:

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<iostream>
    using namespace std;
    inline const int read(){
        register int x=0,f=1;
        register char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
        return x*f;
    }
    const int N=1e3+10;
    const int M=1e5+10;
    const int inf=1e9;
    struct asd{
        int v,w,next;
    }e1[M],e2[M];
    bool vis[N];
    struct node{
        int id;///当前节点编号
        int f;//f表示经过当前节点的最短路
        int g;//g表示S->当前节点的最短路
        node(int id=0,int f=0,int g=0):id(id),f(f),g(g){}
        bool operator <(const node &a)const{
            if(f==a.f) return g>a.g;
            return f>a.f;
        }
    };
    int tot,n,m,K,head1[N],head2[N];
    int dis[N];//dis[i]表示当前点i到终点T的最短路径 
    void add(int x,int y,int z){
        ++tot;
        e1[tot].v=y;e1[tot].w=z;e1[tot].next=head1[x];head1[x]=tot;
        e2[tot].v=x;e2[tot].w=z;e2[tot].next=head2[y];head2[y]=tot;
    }
    void Spfa(int S){//更新每个点->n点的最短距离
        queue<int>q;
        for(int i=1;i<=n;i++) dis[i]=inf;
        dis[S]=0;
        vis[S]=1;
        q.push(S);
        while(!q.empty()){
            int x=q.front();q.pop();
            vis[x]=0;
            for(int i=head2[x];i;i=e2[i].next){
                int v=e2[i].v,w=e2[i].w;
                if(dis[v]>dis[x]+w){
                    dis[v]=dis[x]+w;
                    if(!vis[v]){
                        vis[v]=1;
                        q.push(v); 
                    }
                }
            }
        } 
    }
    void A_Star(int S,int T){
        if(S==T) K++;//
        priority_queue<node>q;
        q.push(node(S,0,0));
        int cnt=0;
        while(!q.empty()){
            node h=q.top();q.pop();
            if(h.id==T){
                if(++cnt==K){
                    printf("%d",h.f);//返回第k短路
                    return ;
                }
            }
            for(int i=head1[h.id];i;i=e1[i].next){
                q.push(node(e1[i].v,h.g+e1[i].w+dis[e1[i].v],h.g+e1[i].w));//最短路更新k短路 
            }
        }
        puts("-1");
    }
    int main(){
        n=read();m=read();
        for(int i=1,x,y,z;i<=m;i++){
            x=read();y=read();z=read();
            add(x,y,z);
        }
        int S,T;
        S=read();T=read();K=read();
        Spfa(T);//预处理,反向遍历 
        A_Star(S,T);
        return 0;
    }
  • 相关阅读:
    mysql常见的hint
    SQL优化:一篇文章说清楚Oracle Hint的正确使用姿势
    Oracle中常见的Hint(一)
    oracle中hint 详解
    Oracle hint 详解
    neo4j简单学习
    Maven的pom.xml文件结构之基本配置parent和继承结构
    使用TASM编译COFF格式和连接
    使用双引擎,让kbmmw 的客户端访问更方便(既给浏览器做服务,也给桌面程序做服务)
    成大事者不纠结(碰到难办的事情的时候,要就事论事,专注当下,放下过去,不忧未来,也不要记仇。防范之举要节制。是做事情的其中一种策略,而且还要分场合)
  • 原文地址:https://www.cnblogs.com/shenben/p/5880307.html
Copyright © 2011-2022 走看看