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短路)的更多相关文章

  1. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  2. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

  3. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  4. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  5. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  6. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

  7. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  8. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  9. POJ 2449 Remmarguts' Date (第k短路径)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:35025   Accepted: 9467 ...

  10. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

随机推荐

  1. CSS3的Animation

    1.animation-name :动画名    2.animation-duration:时间    3.animation-delay:延时    4.animation-iteration-co ...

  2. 使用 Notapad++ 进行 Java 开发

    准备工具 1.安装 JDK 以及配置相关环境变量: 2.安装 64 位版的 Notepad++ : 2.一台 64 位 Windows 系统电脑: 一.下载&安装Notepad++ 官网下载地 ...

  3. Chromium源码系列一:Chromium简介及源代码获取和编译

    Chromium源码系列一:Chromium简介及源代码获取和编译 Chromium简介 ​ Chromium是一个由Google主导开发的网页浏览器,以BSD许可证等多重自由版权发行并开放源代码.C ...

  4. VS2015卸载方法

    VS2015卸载 直接再控制面板的卸载程序中找到 VS2015 的程序,邮件更改,安装程序会被打开,里面有三个选项包含卸载,点击卸载[记得在卸载前如果有打开过 VS 最好重启一下,重启后不要打开 VS ...

  5. Visual Studio 各个版本汇总

    微软开发人员,对开发工具的熟练程度,在一定程度上说明了开发 版本 名称 内部版本 发布日期 支持 .NET Framework 版本 备注 引入 .NET Framework 前[4]  1 Visu ...

  6. nginx配置优化-生产环境应用版

    user www www; worker_processes auto; worker_cpu_affinity auto; error_log /usr/local/nginx/logs/error ...

  7. C#WinFrom写的拼图游戏

    1.窗口载入时自动生成拼图按钮 ;//按钮的行.列数 Button[,] buttons = new Button[N, N];//按钮的数组 ;//记录步数 private void Form3_L ...

  8. April 13 2017 Week 15 Thursday

    Happiness takes no account of time. 幸福不觉光阴过. Do you know the theory of relativity? If you know about ...

  9. 如何使用ABAP代码反序列化JSON字符串成ABAP结构

    假设我有这个JSON字符串如下图所示: 我的任务是解析出上图黑色方框里的几个字段,比如ObjectID, ETag, BuyerID, DateTime, ID, Name等等,把它们的值存储到对应A ...

  10. 字符串处理(POJ1782)

    题目链接:http://poj.org/problem?id=1782 解题报告: #include <iostream> #include <cstdio> #include ...