zoukankan      html  css  js  c++  java
  • Remmarguts' Date(k短路问题)

    Remmarguts’ Date

    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//n m
    1 2 5
    2 1 4
    1 2 2//s t k
    Sample Output:
    14

    基本题意:
    有n个点 m条边
    让你找出s到t的第k短路,如果没有,输出-1
    有多组数据
    思路:
    k短路问题模板
    http://blog.csdn.net/cax1165/article/details/52493456

    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int maxn=100010;
    int n,m,dis[maxn];
    int tot,head1[maxn],head2[maxn];
    bool flag[maxn];
    struct edge
    {
        int to;
        int w;
        int next;
    }e[maxn*2],e2[maxn*2];
    struct node
    {
        int f;
        int g;
        int from;
        bool operator < (node a)const
        {
            if(a.f==f)
            return g>a.g;
            return f>a.f;
        }
    };
    void add_edge(int u,int v,int w)
    {
        tot++;
        e[tot].to=v;
        e[tot].w=w;
        e[tot].next=head1[u];
        head1[u]=tot;
        e2[tot].to=u;
        e2[tot].w=w;
        e2[tot].next=head2[v];
        head2[v]=tot;
    }
    void prepare()
    {
        for(int i=1;i<=n;i++)
        dis[i]=maxn;tot=0;
        memset(head1,0,sizeof(head1));
        memset(head2,0,sizeof(head2));
    }
    void spfa(int t)
    {
        for(int i=1;i<=n;i++)
        dis[i]=maxn;
        dis[t]=0;
        queue<int> q;
        q.push(t);
        flag[t]=1;
        while(!q.empty())
        {
            int v=q.front();
            q.pop();flag[v]=0;
            for(int i=head2[v];i;i=e2[i].next)
            if(dis[e2[i].to]>dis[v]+e2[i].w)
            {
                dis[e2[i].to]=dis[v]+e2[i].w;
                if(!flag[e2[i].to])
                {
                    q.push(e2[i].to);
                    flag[e2[i].to]=1;
                }
            }
        }
    }
    int a_star(int s,int t,int k)
    {
        if(s==t) k++;
        if(dis[s]==maxn) return -1;
        priority_queue<node> q;
        int cnt=0;
        node tmp,to;
        tmp.from=s;
        tmp.g=0;
        tmp.f=tmp.g+dis[tmp.from];
        q.push(tmp);
        while(!q.empty())
        {
            tmp=q.top();
            q.pop();
            if(tmp.from==t) cnt++;
            if(cnt==k) return tmp.g;
            for(int i=head1[tmp.from];i;i=e[i].next)
            {
                to.from=e[i].to;
                to.g=tmp.g+e[i].w;
                to.f=to.g+dis[to.from];
                q.push(to);
            }
        }
        return -1;
    }
    int main()
    {
        int x,y,z,s,t,k;
        while(cin>>n>>m)
        {
            prepare();
            for(int i=1;i<=m;i++)
            {
                cin>>x>>y>>z;
                add_edge(x,y,z);
            }
            cin>>s>>t>>k;
            spfa(t);
            int ans=a_star(s,t,k);
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    git 使用 VisualStudio 比较分支更改
    Java实现 LeetCode 264 丑数 II(二)
    PHP error_log() 函数
    PHP error_get_last() 函数
    PHP debug_print_backtrace() 函数
    PHP debug_backtrace() 函数
    PHP scandir() 函数
    复制相关参数学习笔记--master上的参数
    OI生涯回忆录 2018.11.12~2019.4.15
    C# 金额转中文大写
  • 原文地址:https://www.cnblogs.com/cax1165/p/6070962.html
Copyright © 2011-2022 走看看