poj2449 Remmarguts' Date
来源:互联网 发布:ubuntu启动anaconda 编辑:程序博客网 时间:2024/06/07 13:15
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.
"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).
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 21 2 52 1 41 2 2
Sample Output
14
本题就一个求第K短路径。
代码:
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <vector>#include <queue>#include <cstdio>#include <cmath>#include <string>#include <stack>using namespace std;const int maxn=1001;const int maxm=100001;const int inf=1<<30;struct edge { int from,to,next,w;};edge e1[maxm];edge e2[maxm];int head1[maxn];int head2[maxn];int dist[maxn];bool vis[maxn];struct node { //存储搜索过程中点的信息 int to; int g,f;//f=g+h bool operator <(const node &r) const { if(r.f==f) return r.g<g; else return r.f<f; }};int n,m,t1,t2,k,cnt,s,end;//第k短路void add1(int i,int j,int w) { e1[t1].from=i; e1[t1].to=j; e1[t1].w=w; e1[t1].next=head1[i]; head1[i]=t1++;}void add2(int i,int j,int w) { e2[t2].from=i; e2[t2].to=j; e2[t2].w=w; e2[t2].next=head2[i]; head2[i]=t2++;}void spfa(int s) { //求出路径当前值到终点的最短距离 queue <int> q; q.push(s); for(int i=1; i<=n; i++) dist[i]=inf; memset(vis,false,sizeof(vis)); dist[s]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false;//用vis来标记点是否在队列中 for(int i=head2[u]; i!=-1; i=e2[i].next) { int v=e2[i].to; if(dist[v]>dist[u]+e2[i].w) { dist[v]=dist[u]+e2[i].w; if(!vis[v]) { q.push(v); vis[v]=true; } } } }}int A(int s,int end,int k) { int cnt=0; node e,ne; e.g=0; priority_queue <node> que; if(s==end) k++;//特判 if(dist[s]==inf) return -1; e.to=s; e.f=e.g+dist[e.to];//估价函数的公式 que.push(e); while(!que.empty()) { e=que.top(); que.pop(); if(e.to==end) cnt++;//统计终点出队的次数 if(cnt==k) return e.g;//计算t出队的次数,如果t出对的次数=k,则当前的路径长度g记为第k短路 for(int i=head1[e.to]; i!=-1; i=e1[i].next) { ne.to=e1[i].to; ne.g=e.g+e1[i].w; ne.f=ne.g+dist[ne.to]; que.push(ne); } } return -1;}int main() { int u,v,w; while(scanf("%d%d",&n,&m)) { memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); memset(e1,0,sizeof(e1)); memset(e2,0,sizeof(e2)); t1=t2=0; for(int i=1; i<=m; i++ ) { scanf("%d%d%d",&u,&v,&w); add1(u,v,w); add2(v,u,w); } scanf("%d%d%d",&s,&end,&k); spfa(end); int ans=A(s,end,k); printf("%d\n",ans); } return 0;}
- POJ2449:Remmarguts' Date
- poj2449 Remmarguts' Date
- poj2449 Remmarguts' Date
- poj2449 Remmarguts' Date A*
- poj2449 Remmarguts' Date
- 【Astar】poj2449 Remmarguts' Date
- POJ2449 Remmarguts' Date
- poj2449 Remmarguts' Date
- poj2449 Remmarguts' Date
- POJ2449 Remmarguts' Date
- POJ2449 Remmarguts' Date
- POJ2449 Remmarguts' Date
- POJ2449-Remmarguts' Date
- POJ2449 Remmarguts' Date 【k短路】
- poj2449 Remmarguts' Date (spfa+A*)
- 【A*搜索】poj2449 Remmarguts' Date
- poj2449 Remmarguts' Date A*搜索
- poj2449 Remmarguts' Date,第K短路
- UIWebView加载Loading...两种方法
- 把内容写入指定目录指定文件的java文件工具类,支持日期格式目录名的生成
- UIView详解
- EOJ 1255
- oracle保存小数点前为"0"的问题
- poj2449 Remmarguts' Date
- 面试时,如果面试官问你怎样看待加班,应该怎么回答?
- 关于Sqlite数据库Update语句的一点介绍
- Socket数据通信
- 用代码来控制自定义的控件 自动适应横竖屏时的相对位置
- Tomcat7与Tomcat6比较
- (转)C#开源资源大汇总
- spring mongo 下 mapreduce 和 group的用法
- 二维数组内的二分查找<转载>