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

    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 28467   Accepted: 7731

    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短路模版
    输入
    第一行n,m表示点数和边数
    以下m行m条有向边

    最后一行 起点 终点 求第几短路
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #define inf 10000000
    #define maxx 1010
    #define maxn 100010
    using namespace std;
    int n,m,topt;
    int dis[maxx],f[maxx];
    int head[maxx],first[maxx];
    struct node
    {
        int now;
        int dis;
        int tdis;
        bool operator <(node x)const
        {
            return tdis>x.tdis;
        }
    };
    struct edge
    {
        int to;
        int val;
        int next;
    }e[maxn],r[maxn];
    priority_queue<node>q;
    queue<int>p;
    int init()
    {
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    void Clean()
    {
        topt=0;
        memset(first,0,sizeof(first));
        memset(head,0,sizeof(head));
        while(!p.empty())p.pop();
    }
    void add(int x,int y ,int z)
    {
        topt++;
        e[topt].to=y;
        e[topt].val=z;
        e[topt].next=first[x];
        first[x]=topt;
        
        r[topt].to=x;
        r[topt].val=z;
        r[topt].next=head[y];
        head[y]=topt;
    }
    void spfa(int s)
    {
        for(int i=1;i<=n;i++) dis[i]=inf;
        for(int i=1;i<=n;i++) f[i]=0;
        p.push(s);dis[s]=0;f[s]=1;
        while(!p.empty())
        {
            int now=p.front();p.pop();f[now]=0;
            for(int i=head[now];i;i=r[i].next)
            {
                int to=r[i].to;
                if(dis[to]>dis[now]+r[i].val)
                {
                    dis[to]=dis[now]+r[i].val;
                    if(!f[to])
                    {
                        f[to]=1;
                        p.push(to);
                    }
                }
            }
        }
    }
    int SPFA(int s,int t,int w)
    {
        while(!q.empty())q.pop();
        int tot=0;if(s==t)w++;
        if(dis[s]>=inf)return -1;
        q.push((node){s,0,dis[s]});
        while(!q.empty())
        {
            node x=q.top();q.pop();
            if(x.now==t)tot++;
            if(tot==w)return x.dis;
            for(int i=first[x.now];i;i=e[i].next)
            {
                node to;
                to.now=e[i].to;
                to.dis=x.dis+e[i].val;
                to.tdis=to.dis+dis[to.now];
                q.push(to);
            }
        }
        return -1;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            Clean();
            for(int i=1;i<=m;i++)
            {
                int x,y,z;
                x=init();y=init();z=init();
                add(x,y,z);
            }
            int s,t,w;
            s=init();t=init();w=init();spfa(t);
            printf("%d
    ",SPFA(s,t,w));
        }
        return 0;
    } 
    
    
    
     
  • 相关阅读:
    WebDriver(C#)之十点使用心得
    selenium加载cookie报错问题:selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain
    c#和javascript函数的相互调用(ObjectForScripting 的类必须对 COM 可见。请确认该对象是公共的,或考虑向您的类添加 ComVisible 属性。)
    开源项目推荐:Qt有关的GitHub/Gitee开源项目
    Github Qt开源项目
    QCefView实现与JS的交互
    UE4 Subsystems
    Docker与k8s的恩怨情仇(八)——蓦然回首总览Kubernetes
    表格技术七十二变|手把手教你用Canvas电子表格做电子签名
    应用程序如何通过嵌入式分析技术获益
  • 原文地址:https://www.cnblogs.com/dingmenghao/p/6045566.html
Copyright © 2011-2022 走看看