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/ ...
随机推荐
- tp3.2水印上传文件
<html> <html lang="en"><head> <meta charset="UTF-8"> ...
- vue2.0高仿饿了么better-scroll
首先安装better-scroll npm i better-scroll -S goods页面模板 <template> <div class="goods"& ...
- (C#) Handling and Raising Events
Handling and Raising Events .NET Framework 4.5 Other Versions 6 out of 20 rated this helpful - ...
- 什么是 pwd
pwd print work directory, 指linux terminal的当前目录 $ pwd
- gcc常用参数列举
[参数详解] -c 只激活预处理,编译,和汇编,也就是他只把程序做成obj文件 例子用法: gcc -c hello.c 他将生成.o的obj文件 -S 只激活预处理和编译,就是指 ...
- MySQL入门很简单: 7 触发器
触发器是由事件来触发某个操作,这些事件包括INSERT语句,UPDATE语句和DELETE语句 1.创建触发器 1)创建只有一个执行语句的触发器 例子:再向department表中执行INSERT操作 ...
- I2C总线协议学习笔记 (转载)
1.I2C协议 2条双向串行线,一条数据线SDA,一条时钟线SCL. SDA传输数据是大端传输,每次传输8bit,即一字节. 支持多主控(multimastering),任何时间点只能有一 ...
- 屏蔽firefox浏览器连接失败页面的广告
现象 最近一直在使用firefox浏览器(版本:57.0.1(64位)),同步书签特别方便,但是最近发现当访问的一个不存在的网址时,连接失败页面竟然有广告!firefox不是号称没有广告吗? 分析 F ...
- IOS NSThread 线程间通信
@interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @im ...
- Android(java)学习笔记67:Android Studio新建工程中的build.gradle、settings.gradle
随着信息化的快速发展,IT项目变得越来越复杂,通常都是由多个子系统共同协作完成.对于这种多系统.多项目的情况,很多构建工具都已经提供了不错的支持,像maven.ant.Gradle除了借鉴了ant或者 ...