POJ:2449-Remmarguts' Date(单源第K短路)
Remmarguts’ Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 33081 Accepted: 8993
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 <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e3+10;
vector <pair<int,int> > ve[maxn],ve2[maxn];
int n,m,s,e,k,dis[maxn];
bool vis[maxn];
struct NODE {
int to,g,f;
bool operator < (const NODE &a) const {
if(a.f == f)
return a.g < g;
return a.f < f;
}
};
void init(){
for(int i=0;i<=n;i++) {
ve[i].clear();
ve2[i].clear();
}
for(int i=1;i<=m;i++){
int a,b,len;
scanf("%d%d%d",&a,&b,&len);
ve[a].push_back(make_pair(len,b));
ve2[b].push_back(make_pair(len,a));
}
scanf("%d%d%d",&s,&e,&k);
}
void spfa() {
memset(dis,0x7f,sizeof(dis));
queue <int> qu;
qu.push(e);
dis[e] = 0;
vis[e] = true;
while(!qu.empty()) {
int u = qu.front(); qu.pop();
int d = dis[u];
vis[u] = false;
for(int i=0;i<ve2[u].size();i++) {
int v = ve2[u][i].second;
int d2 = d + ve2[u][i].first;
if(d2 < dis[v]){
dis[v] = d2;
if(!vis[v]) {
qu.push(v);
vis[v] = true;
}
}
}
}
}
int A_star() {
if(dis[s] == 0x7f7f7f7f)
return -1;
NODE temp,Next;
int cnt = 0;
if(s == e)
k++;
temp.to = s; temp.g = 0; temp.f = temp.g + dis[temp.to];
priority_queue <NODE> qu;
qu.push(temp);
while(!qu.empty()){
temp = qu.top(); qu.pop();
if(temp.to == e) cnt++;
if(cnt == k) return temp.g;
int u = temp.to;
int d = temp.g;
for(int i=0;i<ve[u].size();i++) {
Next.to = ve[u][i].second;
Next.g = d + ve[u][i].first;
Next.f = Next.g + dis[Next.to];
qu.push(Next);
}
}
return -1;
}
int main(){
while(scanf("%d%d",&n,&m) != EOF) {
init();
spfa();
int ans = A_star();
printf("%d\n", ans);
return 0;
}
}
POJ:2449-Remmarguts' Date(单源第K短路)的更多相关文章
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- POJ 2449 Remmarguts' Date (第k短路径)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions:35025 Accepted: 9467 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
随机推荐
- (四)JavaScript之[break和continue]与[typeof、null、undefined]
7].break和continue /** * JavaScript 的break和continue语句 * break 跳出switch()语句 * break 用于跳出循环 * continue ...
- (转)ArcEngine读取数据(数据访问)
读取和访问数据是进行任何复杂的空间分析及空间可视化表达的前提,ArcGIS支持的数据格式比较丰富,下面就这些格式Shapefile.Coverage.Personal Geodatabase.Ente ...
- android api 之Scroller
Scroller是封装了滚动,实现View和ViewGroup的背景画布的滚动. 它有两个构造方法: public Scroller (Context context) 传递一个上下文. public ...
- Eclipse+ADT+Android SDK 搭建安卓开发环境(转)
要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); eclipse-jee-luna-SR2-win32(32位);ADT-23.0.4 下载地址 环境下载 最近 ...
- linux脚本的source和reload
什么时候用reload?有些程序, 当你修改了配置文件后, 需要重启之后, 配置才能生效,但是 这个程序又不能 重启 , 如大公司的httpd服务 因此, 当你修改完了之后, 需要在不重启服务的情况下 ...
- sharepoint2010的几个类型字段赋值和取值的方法
1.日期类型查询,需要转换,方法如下: //转换时间 string startdate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Date ...
- 2017年10月31日结束Outlook 2007与Office 365的连接
2017 年10月31日 ,微软即将推出 Office 365中Exchange Online邮箱将需要Outlook for Windows的连接,即通过HTTP Over MAPI方式,传统使用R ...
- MySQL报错:Packets larger than max_allowed_packet are not all
MySQL根据配置文件会限制Server接受的数据包大小.有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败. 修改方法: 1.修改配置文件my.ini m ...
- Win7无法连接wifi网络的解决方法
以下方法是一个笔记,不能保证100%解决问题 方法1. 在CMD命令窗口中, ipconfig /release ipconfig/renew 方法2. 右键点网络图标,troubleshoot pr ...
- Aizu The Maximum Number of Customers
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_A The Maximum Number of Customers Ide ...