poj2449

Remmarguts‘ Date

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 27699   Accepted: 7500

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

题解:

k短路模板

AC代码:

#include<cstdio>
#include<queue>
using namespace std;
const int N=50010;
struct node{
    int v,next;
    int w;
}e1[N*10],e2[N*10];
int tot,n,m,k,head1[N],head2[N];
int dis[N];
bool vis[N];
struct data{
    int f,g;
    int from;
    data(int x,double y,double z):from(x),f(y),g(z){}
    bool operator < (const data &a) const {
        if(f==a.f) return g>a.g;
        return f>a.f;
    }
};
inline void read(int &x){
    register char ch=getchar();x=0;
    while(ch>‘9‘||ch<‘0‘) ch=getchar();
    while(ch>=‘0‘&&ch<=‘9‘) x=(x<<3)+(x<<1)+ch-‘0‘,ch=getchar();
}
void add(int x,int y,int z){
    ++tot;
    e1[tot].v=y;e1[tot].w=z;e1[tot].next=head1[x];head1[x]=tot;
    e2[tot].v=x;e2[tot].w=z;e2[tot].next=head2[y];head2[y]=tot;
}
void spfa(int S){
    for(int i=1;i<=n;i++) dis[i]=0x3f3f3f3f;
    dis[S]=0;
    queue<int>q;
    q.push(S);
    vis[S]=1;
    while(!q.empty()){
        int h=q.front();q.pop();
        vis[h]=0;
        for(int i=head2[h];i;i=e2[i].next){
            int v=e2[i].v,w=e2[i].w;
            if(dis[v]>dis[h]+w){
                dis[v]=dis[h]+w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
void a_star(int S,int T){
    priority_queue<data>q;
    if(S==T) k++;
    int cnt=0;
    q.push(data(S,dis[S],0));
    while(!q.empty()){
        data h=q.top();q.pop();
        if(h.from==T){
            if(++cnt==k){
                printf("%d",h.f);
                return ;
            }
        }
        for(int i=head1[h.from];i;i=e1[i].next){
            q.push(data(e1[i].v,h.g+e1[i].w+dis[e1[i].v],h.g+e1[i].w));//最短路更新k短路
        }
    }
    puts("-1");
}
int main(){
    int x,y,z,S,T;
    read(n);read(m);
    for(int i=1;i<=m;i++){
        read(x);read(y);read(z);
        add(x,y,z);
    }
    read(S);read(T);read(k);
    spfa(T);
    a_star(S,T);
    return 0;
}
时间: 2024-10-22 08:18:16

poj2449的相关文章

【poj2449】 Remmarguts&#39; Date

http://poj.org/problem?id=2449 (题目链接) 题意 求有向图K短路. Solution A*.g(x)为当前节点到起点的步数,h(x)为当前节点到终点的最短距离(也就是估价函数). 细节 dijkstra求终点到各点最短路时要把边反向.原来起点和终点可以是同一个点,坑死了... 代码 // poj2499 #include<algorithm> #include<iostream> #include<cstdlib> #include<

poj2449 Remmarguts&#39; Date,第K短路

点击打开链接 SPFA  + A* #include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; struct node { int v, dis, f, next; friend bool operator <(node a, node b){ return a.f>b.f; } }; const int INF =

POJ2449 Remmarguts&#39; Date

题目链接:http://poj.org/problem?id=2449 题目描述: 其实题目的大意就是求 第k短路, 存在就输出, 不存在就输出-1. 注意当起点和终点一致的时候,需要k++, 因为在OUTPUT时提到the length (time required) to welcome Princess Uyuw using the K-th shortest path. 可以看出是需要时间的,并且并没有描述相同点直接的时间为0这一条件,因此在计算路径的时候,为0的路径是不能够算到里面的:

[poj2449]Remmarguts&#39; Date(spfa+A*)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 21855   Accepted: 5958 Description "Good man never makes girls wait or breaks an appointment!" said the mand

POJ2449 Remmarguts&#39; Date 【k短路】

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 21064   Accepted: 5736 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

POJ2449 Remmarguts&#39; Date 第K短路

POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即为第K短路 代码也很简单 //数组开的不够 不一定是运行时错误! 可能也会WA #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath&g

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

(WA)POJ2449 第K短路

POJ2449 第K短路 改了好长时间发现读入读反了qwq A*,先在反向图上求出每个点到t的最短距离,作为估价函数即可 疑问:能不能直接记录h+g 1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 #include <queue> 6 #include <iostream> 7 using namespace s

poj2449:第k短路问题

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

poj2449第K短路问题(A*算法)

启发函数:f(x)=g(x)+h(x); g(x)表示初始点到x状态的代价,h(x)表示从x的状态到目标状态的代价的估计值(并不是真实的),实际最小代价<=h(x); 起点s,终点t,x.v=s,x.len=0;然后优先队列中f(x)值最小值出队列,再根据出队列的x.v状态发展下一层.如果出队列时第一次遇到x.v==t, 就找到了s到t的最短路....如果第k次,那就是第k短.为了加速计算,h(p)需要在A*搜索之前进行预处理,只要将原图的所有边反向, 再从终点t做一次单源点最短路径就能得到每个