zoukankan      html  css  js  c++  java
  • POJ 2449 第K短路

    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 36881   Accepted: 10169

    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

    解析 第K短路裸题

    #include <iostream>
    #include <string.h>
    #include <queue>
    #include <stdio.h>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define all(a) (a).begin(), (a).end()
    #define fillchar(a, x) memset(a, x, sizeof(a))
    #define huan printf("
    ");
    using namespace std;
    typedef long long ll;
    const ll inf = 0x7f7f7f7f;
    const int maxn=1e5+10;
    const int mod=1e9+7;
    using namespace std;
    ll num1=0,num2=0,n,m,s,t,r,k,h1[maxn],h2[maxn],d[maxn],flag[maxn];
    struct node1
    {
        ll x,y,z,next;
    } mp1[maxn],mp2[maxn];
    struct node
    {
        ll v,c;
        node(ll vv,ll cc) : v(vv),c(cc) {}
        friend bool operator < (node x,node y)
        {
            return x.c+d[x.v]>y.c+d[y.v];
        }
    };
    void init()
    {
        num1=0;
        num2=0;
        fillchar(h1,0);
        fillchar(h2,0);
    }
    inline void insert1(ll x,ll y,ll z)
    {
        mp1[++num1].x=x;
        mp1[num1].y=y;
        mp1[num1].z=z;
        mp1[num1].next=h1[x];
        h1[x]=num1;
    }
    inline void insert2(ll x,ll y,ll z)
    {
        mp2[++num2].x=x;
        mp2[num2].y=y;
        mp2[num2].z=z;
        mp2[num2].next=h2[x];
        h2[x]=num2;
    }
    void spfa()
    {
        queue<ll>Q;
        Q.push(t);
        for(int i=1; i<=n; i++)
            d[i]=inf;
        d[t]=0;
        fillchar(flag,0);
        flag[t]=1;
        while(!Q.empty())
        {
            ll u=Q.front();
            Q.pop();
            flag[u]=0;
            for(int i=h1[u]; i; i=mp1[i].next)
            {
                ll v=mp1[i].y;
                if(d[v]>d[u]+mp1[i].z)
                {
                    d[v]=d[u]+mp1[i].z;
                    if(!flag[v])
                    {
                        flag[v]=1;
                        Q.push(v);
                    }
                }
            }
        }
    }
    ll astar()
    {
        if(d[s]==inf)
            return -1;
        priority_queue<node>p;
        ll cnt=0;
        p.push(node(s,0));
        while(!p.empty())
        {
            node u=p.top();
            p.pop();
            if(u.v==t)
            {
                cnt++;
                if(cnt==k)
                    return u.c;
            }
            for(int i=h2[u.v]; i; i=mp2[i].next)
            {
                ll y=mp2[i].y;
                p.push(node(y,u.c+mp2[i].z));
            }
        }
        return -1;
    }
    int main()
    {
        while(scanf("%lld%lld",&n,&m)!=EOF)
        {
            init();
            //read(s);read(t);read(k);read(r);
            while(m--)
            {
                ll x,y,z;
                scanf("%lld%lld%lld",&x,&y,&z);
                insert1(y,x,z);
                insert2(x,y,z);
            }
            scanf("%lld%lld%lld",&s,&t,&k);
            if(s==t)   //s到t认为不可达k++
                k++;
            spfa();
            ll _=astar();          //返回-1代表不能到达
            cout<<_<<endl;     //输出第k短路
        }
        return 0;
    }
  • 相关阅读:
    ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
    ASP.NET MVC+EasyUI+Entity FrameWork 整合开发
    ASP.NET MVC局部验证及相关问题
    asp.net mvc常用的数据注解和验证以及entity framework数据映射
    Entity Framework 一次加载许多个 Fluent API 映射
    Entity Framework Code First 常用方法集成
    ASP.NET MVC 应用程序的安全性,看一眼你就会了
    Entity Framework SqlFunctions 教你如何在EF调用sqlserver方法的函数存根
    ASP.NET MVC 及 Areas 简单控制路由
    ASP.NET MVC 表单的几种提交方式
  • 原文地址:https://www.cnblogs.com/stranger-/p/9610442.html
Copyright © 2011-2022 走看看