• 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;
    } 
    
    
    
     
  • 相关阅读:
    python全栈开发day20-类的三大特性继承、多态、封装
    python全栈开发day19-面向对象初识
    python全栈开发day21-2 几个装饰器总结
    python全栈开发day16-正则表达式和re模块
    python全栈开发day15-递归函数、二分查找
    python运算符优先级
    动手动脑4
    动手动脑3
    查询对象个数
    动手动脑2
  • 原文地址:https://www.cnblogs.com/dingmenghao/p/6045566.html
Copyright © 2020-2023  润新知