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. jquery常用属性与方法

    1..css( )给指定的样式设置样式值: 2..attr(attributeName,value) /.removeAttr(attributeName);给指定的属性设置值 / 清除所有匹配的元素 ...

  2. angular解决压缩问题,和传送数据

    1.angular解决压缩的方法 var app = angular.module("mk",[]); app.controller("ctrl",['$sco ...

  3. ArcMap如何修改地图坐标系统

    有时候,地图投影坐标需要作出修改,使得符合要求,不然空间参考不一样无法进行进一步的操作,分析等!下面介绍arcgis地图投影坐标的修改! 1.首先,将地图数据导入,这里我导入的是广西的边界图bound ...

  4. 安装office提示Office 16 Click-to-Run Extensibility Component

    今天安装office时,提示Office 16 Click-to-Run Extensibility Component或者Office 15 Click-to-Run Extensibility C ...

  5. Asp.net防御XSS攻击组件库

    一.AntiXss 翻看mvc4高级编程,偶看到作者强烈推荐使用AntiXss防御XSS攻击,收集资料看下. 目前类库已融入到.netframework中,类库主页不再更新. 使用方法:使用Nuget ...

  6. SharePoint 2010 列表查阅项栏的formfield控件对象取值

    开发的时候想当然的认为主表解析出来就是一个dropdownlist,可是在大数据测试的时候,发现有情况. 首先创建一个子列表:DetailList,并添加19条数据: 创建主列表:MainList,并 ...

  7. 07、Spark集群的进程管理

    07.Spark集群的进程管理 7.1 概述 Spark standalone集群模式涉及master和worker两个守护进程.master进程是管理节点,worker进程是工作节点.spark提供 ...

  8. 【js基础修炼之路】- 手把手教你实现bind

    手写bind前我们先回顾一下bind有哪些特性,以便更好的理解bind和实现bind. bind的特性 var obj = { a: 100, say(one, two) { console.log( ...

  9. Linux同步目录 保留文件修改时间和权限 rsync

    scp copy文件夹的时候,会强行覆盖文件,导致增量同步的时候不方便,而rsync则能很好解决这个问题. rsync -avz ubuntu@192.168.1.208:/home/ubuntu/m ...

  10. ELF文件格式与进程地址空间的联系

    http://blog.csdn.net/q_l_s/article/details/52597330 三.分析在fork产生新进程中ELF文件格式与进程地址空间的联系 1.进程的虚拟地址空间 每个程 ...