zoukankan      html  css  js  c++  java
  • POJ 2449 Remmarguts' Date

    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 30725   Accepted: 8389

    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
     
     
    丧心病狂的一道题!
    丫的卡我的scanf!
    k短路模版
    做完这题大脑彻底短路了。
    过程非常艰辛。。
    从MLE RE 到 WA 的时候 我好激动啊。。

    屠龙宝刀点击就送

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #define M 100005
    #define N 1005
    using namespace std;
    struct node
    {
        int to,f,g;
        bool operator<(node a)const
        {
            if(f==a.f) return g>a.g;
            return f>a.f;
        }
    };
    struct Edge
    {
        int next,to,val;
    };
    Edge edge1[M*2],edge2[M*2];
    int n,m,dist[N],head1[N],head2[N],cnt;
    bool vis[N];
    void spfa(int s)
    {
        queue<int>Q;
        for(int i=1;i<=n;++i) dist[i]=M,vis[i]=0;
        dist[s]=0;
        Q.push(s);
        vis[s]=1;
        while(!Q.empty())
        {
            int now=Q.front();Q.pop();
            vis[now]=0;
            for(int i=head2[now];i;i=edge2[i].next)
            {
                int v=edge2[i].to;
                if(dist[v]>dist[now]+edge2[i].val)
                {
                    dist[v]=dist[now]+edge2[i].val;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        Q.push(v);
                    }
                }
            }
        }
    }
    inline void ins(int u,int v,int w)
    {
        edge1[++cnt].next=head1[u];
        edge1[cnt].to=v;
        edge1[cnt].val=w;
        head1[u]=cnt;
        edge2[cnt].next=head2[v];
        edge2[cnt].to=u;
        edge2[cnt].val=w;
        head2[v]=cnt;
    }
    int Astar(int s,int t,int k)
    {
        priority_queue<node>Q;
        if(s==t) k++;
        int cnt=0;
        if(dist[s]==M) return -1;
        node a,tmp;
        a.to=s;
        a.g=0;
        a.f=a.g+dist[a.to];
        Q.push(a);
        while(!Q.empty())    
        {
            node now=Q.top();Q.pop();
            if(now.to==t) cnt++;
            if(cnt==k) return now.g;
            for(int i=head1[now.to];i;i=edge1[i].next)
            {
                int v=edge1[i].to;
                tmp.to=v;
                tmp.g=now.g+edge1[i].val ;
                tmp.f=tmp.g+dist[tmp.to];
                Q.push(tmp);
            }
        }
        return -1;
    }
    inline void init()
    {
        cnt=0;
        memset(head1,0,sizeof(head1));
        memset(head2,0,sizeof(head2));
    }
    int main()
    {
        for(int s,t,k;cin>>n>>m;)
        {
            init();
            for(int x,y,z;m--;)
            {
                cin>>x>>y>>z;
                ins(x,y,z); 
            }
            cin>>s>>t>>k;
            spfa(t);
            int ans=Astar(s,t,k);
            cout<<ans<<endl;
        }
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    sql处理数据库锁的存储过程
    SQL语句
    partial 函数
    map函数
    python命令行上下 退格,左右键不能用
    postgresql 在linux上的源码安装
    python字典操作
    根据key存不存在查询json
    精典博文
    python解析XML之ElementTree
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7412942.html
Copyright © 2011-2022 走看看