ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(约束第K短路)
题意:求11到nn的第kk短的路径长度,如果超过TT输出Whitesnake!Whitesnake!,否则输出yareyaredawayareyaredawa。
好无以为 , 这就是一道模板题, 当是与之不同的是有T的限制,还要注意是在所有满足条件的边中的第k条 , 比赛的时候题意理解问题,导致无法AC
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
const int maxm = ;
int TIME;
struct EDGE{ int v, nxt, w; };
struct NODE{
int pos, Cost, F;
bool operator < (const NODE & rhs) const {//重载的时候注意符号
if(this->F == rhs.F) return this->Cost > rhs.Cost;
return this->F > rhs.F;
};
}; EDGE Edge[maxm];
EDGE REdge[maxm];
int Head[maxn], RHead[maxn];
int cnt, Rcnt;
int N;
void init()
{
memset(Head, -, sizeof(Head));
memset(RHead, -, sizeof(RHead));
cnt = Rcnt = ;
} void AddEdge(int from, int to, int weight)
{
Edge[cnt].w = weight;
Edge[cnt].v = to;
Edge[cnt].nxt = Head[from];
Head[from] = cnt++;
} void AddREdge(int from, int to, int weight)
{
REdge[Rcnt].w = weight;
REdge[Rcnt].v = to;
REdge[Rcnt].nxt = RHead[from];
RHead[from] = Rcnt++;
} int vis[maxn];
int H[maxn];
void SPFA(int st)
{
queue<int> que;
memset(H, INF, sizeof(H));
memset(vis, , sizeof(vis));
H[st] = ;
que.push(st);
while(!que.empty()){
int cur = que.front(); que.pop();
vis[cur] = ;
for(int i=RHead[cur]; i!=-; i=REdge[i].nxt) {
int v = REdge[i].v;
if(H[v] > H[cur] + REdge[i].w) {
H[v] = H[cur] + REdge[i].w;
if(!vis[v]) {
vis[v] = ;
que.push(v);
}
}
}
}
} int A_Star(int s, int t, int k)
{
if(s == t) k++;
if(H[s]==INF) return -;
priority_queue<NODE> que;
NODE cur, into;
cur.pos = s;
cur.Cost = ;
cur.F = H[s];
que.push(cur);
int CNT = ;
while(!que.empty()){
cur = que.top();
que.pop();
if(cur.Cost>TIME)///因为是最优最优的来,不条件的话,下面的也肯定吧满足,
return -;
if(cur.pos == t) CNT++;
if(CNT == k) return cur.Cost;
for(int i=Head[cur.pos]; i!=-; i=Edge[i].nxt){
into.Cost = cur.Cost+Edge[i].w;
into.F = cur.Cost+Edge[i].w+H[Edge[i].v];
into.pos = Edge[i].v;
que.push(into);
}
}return -;
} int main(void)
{
int M, K, S, des;
while(~scanf("%d %d", &N, &M)){
init();
int from, to, weight;
scanf("%d %d %d %d", &S, &des, &K,&TIME);
while(M--){
scanf("%d %d %d",&from, &to, &weight);
AddEdge(from, to, weight);
AddREdge(to, from, weight);//建反向边
} SPFA(des);//求其他点到终点的最短路,作为H值
if(A_Star(S,des,K)!=-)
puts("yareyaredawa");
else
puts("Whitesnake!");
}
return ;
}
ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(约束第K短路)的更多相关文章
- ACM-ICPC 2018 沈阳赛区网络预赛 D Made In Heaven(第k短路,A*算法)
https://nanti.jisuanke.com/t/31445 题意 能否在t时间内把第k短路走完. 分析 A*算法板子. #include <iostream> #include ...
- ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(第k短路模板)
求第k短路模板 先逆向求每个点到终点的距离,再用dij算法,不会超时(虽然还没搞明白为啥... #include<iostream> #include<cstdio> #inc ...
- 图上两点之间的第k最短路径的长度 ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven
131072K One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. Howe ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number
Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...
- ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)
Made In Heaven One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J树分块
J. Ka Chang Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero p ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number
A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
随机推荐
- POJ2774Long Long Message (后缀数组&后缀自动机)
问题: The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to ...
- RenderingPath 渲染路径
http://blog.csdn.net/lichaoguan/article/details/42554821 RenderingPath 渲染路径 Deferred Lighting 延时光照 延 ...
- 洛谷【P2201】数列编辑器
我对模拟的理解:http://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.luogu.org/problemnew/show/P220 ...
- bzoj 2044 三维导弹拦截——DAG最小路径覆盖(二分图)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2044 还以为是CDQ.发现自己不会三维以上的…… 第一问可以n^2.然后是求最长不下降子序列 ...
- docker 容器内服务自启动
centos6/7 有区别先说6 centos6: 方式一:rc.local + 容器内的启动脚本 1.chkconfig docker on #docker开机自启动2.[root@master y ...
- 什么是Nginx?为什么使用Nginx?
源自 https://blog.csdn.net/yougoule/article/details/78186138 一.前言 为毛要用nginx服务器代理,不直接用tomcat 7.0,还 ...
- ssh功能模块——paramiko
参考官网文档:http://docs.paramiko.org/
- C++之string类
1.String对象的初始化 string s1; 默认构造函数,s1为空串 string s4(n, 'c'); 将s4初始化为字符c的n个副本 string s ...
- 【总结整理】javascript进阶学习(慕课网)
数组 数组是一个值的集合,每个值都有一个索引号,从0开始,每个索引都有一个相应的值,根据需要添加更多数值. 二维数组 二维数组 一维数组,我们看成一组盒子,每个盒子只能放一个内容. 一维数组的表示: ...
- Java Learning 001 新建一个Java工程 HelloWorld程序
Java Learning 001 新建一个Java工程 HelloWorld程序 Step 1 . 在Eclipse 软件里,点击: File -> New -> Java Projec ...