POJ2449-Remmarguts' Date
来源:互联网 发布:通讯录恢复软件 编辑:程序博客网 时间:2024/06/17 22:52
Remmarguts’ Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 30805 Accepted: 8405
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
题目大意:求第
解题思路:
#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;typedef long long LL;const LL INF=1e18;const int MAXN=1e5+5;int head[MAXN],head1[MAXN];LL dis[MAXN];bool vis[MAXN];int n,m,tot,st,en,k;struct Edge{ int u,v,nxt,nxt1; LL c; Edge(){} Edge(int _u,int _v,LL _c):u(_u),v(_v),c(_c){}}e[MAXN*2];struct qnode{ int v; LL c; qnode(){} qnode(int _v,LL _c):v(_v),c(_c){} bool operator < (const qnode& rhs) const { return c+dis[v]>rhs.c+dis[rhs.v]; }};void addedge(int u,int v,LL c){ e[tot]=Edge(u,v,c); e[tot].nxt=head[u];head[u]=tot; e[tot].nxt1=head1[v];head1[v]=tot++;}void dij(int src){ memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) dis[i]=INF; dis[src]=0; priority_queue<qnode> que; que.push(qnode(src,0)); while(!que.empty()) { qnode pre=que.top(); que.pop(); vis[pre.v]=true; for(int i=head1[pre.v];i!=-1;i=e[i].nxt1) { if(dis[e[i].u]>dis[pre.v]+e[i].c) { dis[e[i].u]=dis[pre.v]+e[i].c; que.push(qnode(e[i].u,0)); } } while(!que.empty()&&vis[que.top().v]) que.pop(); }}LL a_star(int src){ priority_queue<qnode> que; que.push(qnode(src,0)); k--; while(!que.empty()) { qnode pre=que.top();que.pop(); if(pre.v==en) { if(k) k--; else return pre.c; } for(int i=head[pre.v];i!=-1;i=e[i].nxt) que.push(qnode(e[i].v,pre.c+e[i].c)); } return -1;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); memset(head1,-1,sizeof(head1)); tot=0; for(int i=0;i<m;i++) { int u,v;LL c; scanf("%d%d%lld",&u,&v,&c); addedge(u,v,c); //addedge(v,u,c); } scanf("%d%d%d",&st,&en,&k); dij(en); if(st==en) k++; printf("%lld\n",a_star(st)); } 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短路
- AS3 前端网页游戏系统的开发
- (十一)Spring Boot整合Mybatis使用druid实现多数据源自动切换
- HDU 6172 Array Challenge 找规律+矩阵快速幂
- SCARA 机器人概述
- vue标签属性条件渲染
- POJ2449-Remmarguts' Date
- 利用UE简化JAVA后端的SQL语句编辑
- [Leetcode] 12. Integer to Roman
- 嵌入式Linux驱动笔记(九)------dts设备树在2440使用
- java成员变量何时被回收,以及监听回收
- 十大机器学习公开课
- 巧秒的杨辉三角
- UVA
- 算法系列——变态跳台阶(剑指offer)