【POJ】2449 Remmarguts' Date(k短路)

时间:2022-09-06 08:29:26

http://poj.org/problem?id=2449

不会。。

百度学习。。

恩。

k短路不难理解的。

结合了a_star的思想。每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k

首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h

根据f=g+h

我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路)

将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x到t的某个最优路线,将他们加入到堆)

当到达t的点数累计到了k,那么直接输出g值,如果s==t的话,k要先+1再进行astar(因为当s==t的时候,不经过一条边就满足了一个最短路,但这条路不能算,所以要剪掉,那么也就是k要加上1)

为什么呢。。。

因为我们每次操作就相当于拿出一条边,然后匹配它到t的最优路线形成一条s到t的路线。。。哎呀,自己慢慢理解。

// 2015.5.8 upd:听说关键字是g+f而不是f呢QAQ怪不得wc跪掉了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int oo=1000000000, N=1005, M=100005;
int h[N], ihead1[N], ihead2[N], cnt1, cnt2, vis[N], n, m;
struct astr {
int v, g, f;
const bool operator< (const astr &b) const {
return f+g>b.f+b.g;
}
};
struct ED { int v, next, w; }e1[M], e2[M];
priority_queue<astr> pq;
queue<int> q;
inline void add(const int &u, const int &v, const int &w) {
e1[++cnt1].next=ihead1[u]; ihead1[u]=cnt1; e1[cnt1].v=v; e1[cnt1].w=w;
e2[++cnt2].next=ihead2[v]; ihead2[v]=cnt2; e2[cnt2].v=u; e2[cnt2].w=w;
}
void spfa(const int &s) {
int u;
for1(i, 1, n) h[i]=oo;
CC(vis, 0);
h[s]=0; vis[s]=1; q.push(s);
while(!q.empty()) {
u=q.front(); q.pop(); vis[u]=0;
for(int i=ihead2[u]; i; i=e2[i].next) if(h[u]+e2[i].w<h[e2[i].v]) {
h[e2[i].v]=h[u]+e2[i].w;
if(!vis[e2[i].v]) { vis[e2[i].v]=1; q.push(e2[i].v); }
}
}
}
int getans(const int &s, const int &t, int k) {
if(h[s]==oo) return -1;
if(s==t) ++k;
while(!pq.empty()) pq.pop();
int num=0;
astr now, tp; now.v=s; now.g=0; now.f=now.g+h[now.v];
pq.push(now);
while(!pq.empty()) {
now=pq.top(); pq.pop();
if(now.v==t) ++num;
if(num==k) return now.g;
for(int i=ihead1[now.v]; i; i=e1[i].next) {
tp.v=e1[i].v;
tp.g=now.g+e1[i].w;
tp.f=tp.g+h[tp.v];
pq.push(tp);
}
}
return -1;
} int main() {
while(~scanf("%d%d", &n, &m)) {
int u, v, w;
cnt1=cnt2=0; CC(ihead1, 0); CC(ihead2, 0);
rep(i, m) {
read(u); read(v); read(w);
add(u, v, w);
}
read(u); read(v); read(w);
spfa(v);
printf("%d\n", getans(u, v, w));
}
return 0;
}

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

Source

POJ Monthly,Zeyuan Zhu

【POJ】2449 Remmarguts' Date(k短路)的更多相关文章

  1. poj 2449 Remmarguts&&num;39&semi; Date K短路&plus;A&ast;

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

  2. POJ 2449 Remmarguts&&num;39&semi; Date &lpar;K短路 A&ast;算法)

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

  3. POJ 2449 Remmarguts&&num;39&semi; Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  4. poj 2449 Remmarguts&&num;39&semi; Date(第K短路问题 Dijkstra&plus;A&ast;)

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

  5. poj 2449 Remmarguts&&num;39&semi; Date (k短路模板)

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

  6. POJ 2449 - Remmarguts&&num;39&semi; Date - &lbrack;第k短路模板题&rsqb;&lbrack;优先队列BFS&rsqb;

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

  7. 图论&lpar;A&ast;算法,K短路&rpar; :POJ 2449 Remmarguts&&num;39&semi; Date

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

  8. poj 2449 Remmarguts&&num;39&semi; Date 第k短路 (最短路变形)

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

  9. poj 2449 Remmarguts&&num;39&semi; Date(K短路&comma;A&ast;算法)

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

  10. K短路模板POJ 2449 Remmarguts&&num;39&semi; Date

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

随机推荐

  1. 配置了&lt&semi;mvc&colon;resources&gt&semi; 导致以前的controller 无法访问。

    解决方案: <mvc:annotation-driven/>

  2. Swift2&period;0异常处理

    // 在抛出异常之前,我们需要在函数或方法的返回箭头 -> 前使用 throws 来标明将会抛出异常 func myMethodRetrunString() throws -> Strin ...

  3. 一步一步重写 CodeIgniter 框架 &lpar;4&rpar; —— load&lowbar;class 管理多个对象实例的思路

    我们使用CodeIgniter 框架最主要是想利用其 MVC 特性,将模型.视图分开,并通过控制器进行统一控制.在尝试实现 MVC 模式之前,我们将实现其中一个对程序结构非常有用的技巧,就是 load ...

  4. 如何将ASP&period;NET-WebApi发布到IIS6&period;0上(转)

    关于"如何将ASP.NET-WebApi发布到IIS6.0上"的这方面的学习,一开始项目组长让我们接触的时候,我的心情是这样的 哇呜.jpg 当时真的是一脸懵逼啊,对于刚接触asp ...

  5. CSS深入理解学习笔记之relative

    1.relative和absolute的相煎关系 限制作用:①限制left/top/right/bottom定位:②限制z-index层级:③限制在overflow下的嚣张气焰. relative和f ...

  6. C&num;中 SQL语句

    SQL语句 SELECT STUFF(( (SELECT ',{' ),)) ),)) ),)) +'}' FROM ZSJTTD_HouseBuilding hb ,,'' ) HouseBuild ...

  7. SA vs NSA

    5G: What is Standalone (SA) vs Non-Standalone (NSA) Networks? According to the recent 3GPP Release 1 ...

  8. 【Python笔记】十分钟搞定pandas

    本文是对pandas官方网站上<10 Minutes to pandas>的一个简单的翻译,原文在这里.这篇文章是对pandas的一个简单的介绍,详细的介绍请参考:Cookbook .习惯 ...

  9. oracle的sql语句训练

    --查询工资最高的人的名字select ename ,sal from emp where sal=(select max(sal) from emp );--求出员工的工资在所有人的平均工资之上的人 ...

  10. EF中使用Linq的Lambda比较字符串格式日期大小

    在使用EF时,想要比较字符串类型的日期时,参考以下: SQL语句: 1)select * from TableName where StartTime > ‘2015-04-08‘ 2)sele ...