ACM-ICPC 2018 沈阳赛区网络预赛 D Made In Heaven(第k短路,A*算法)
https://nanti.jisuanke.com/t/31445
题意
能否在t时间内把第k短路走完。
分析
A*算法板子。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 1e9;
const int maxn = + ;
const int maxm = + ;
const int mod = ;
int head[maxn],nxt[maxm],head1[maxn],nxt1[maxm];
int dis[maxn];
bool vis[maxn];
int n,m,e,st,en,k,t;
struct note{
int u,v,c;
note(){}
note(int u,int v,int c):u(u),v(v),c(c){}
}p[maxm];
struct POJ{
int v,c;
POJ(){}
POJ(int v,int c):v(v),c(c){}
bool operator < (const POJ& other)const{
return c+dis[v]>other.c+dis[other.v];
}
};
void addnote(int u,int v,int c){
p[e]=note(u,v,c);
nxt[e]=head[u];head[u]=e;
nxt1[e]=head1[v];head1[v]=e++;
}
void dij(int src){
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) dis[i]=inf;
dis[src]=;
priority_queue<POJ> que;
que.push(POJ(src,));
while(!que.empty()){
POJ pre=que.top();que.pop();
vis[pre.v]=true;
for(int i=head1[pre.v];i+;i=nxt1[i]){
if(dis[p[i].u]>dis[pre.v]+p[i].c){
dis[p[i].u]=dis[pre.v]+p[i].c;
que.push(POJ(p[i].u,));
}
}
while(!que.empty()&&vis[que.top().v]) que.pop();
}
}
int a_star(int src){
priority_queue<POJ> que;
que.push(POJ(src,));k--;
while(!que.empty()){
POJ pre=que.top();que.pop();
if(pre.v==en){
if(k) k--;
else return pre.c;
}
for(int i=head[pre.v];i+;i=nxt[i])
if(pre.c+p[i].c<=t)
que.push(POJ(p[i].v,pre.c+p[i].c));
}
return -;
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
while(~scanf("%d%d",&n,&m)){
scanf("%d%d%d%d",&st,&en,&k,&t);
memset(head,-,sizeof(head));
memset(head1,-,sizeof(head1));
e=;
for(int i=;i<m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
addnote(u,v,c);
}
dij(en);
if(dis[st]==inf){
puts("Whitesnake!");
continue;
}
// if(st==en) k++;
int ans=a_star(st);
if(ans==-||ans>t){
puts("Whitesnake!");
}else{
puts("yareyaredawa");
}
// printf("%d\n",a_star(st));
}
return ;
}
ACM-ICPC 2018 沈阳赛区网络预赛 D Made In Heaven(第k短路,A*算法)的更多相关文章
- 图上两点之间的第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 沈阳赛区网络预赛 D. Made In Heaven(第k短路模板)
求第k短路模板 先逆向求每个点到终点的距离,再用dij算法,不会超时(虽然还没搞明白为啥... #include<iostream> #include<cstdio> #inc ...
- ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(约束第K短路)
题意:求11到nn的第kk短的路径长度,如果超过TT输出Whitesnake!Whitesnake!,否则输出yareyaredawayareyaredawa. 好无以为 , 这就是一道模板题, 当是 ...
- 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 ...
随机推荐
- bat(批处理)命令(tomcat 7.0.75 startup.bat 命令集)
本文主要介绍tomcat 7.0.75中startup.bat(位置:tomcat目录\bin)中涉及到的bat命令,为tomcat源码研究做准备. startup.bat中涉及到的bat命令如下: ...
- LeetCode算法题-N-ary Tree Preorder Traversal(Java实现)
这是悦乐书的第268次更新,第282篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第135题(顺位题号是589).给定一个n-ary树,返回其节点值的前序遍历.例如,给定 ...
- LeetCode算法题-Array Partition I(Java实现)
这是悦乐书的第262次更新,第275篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第129题(顺位题号是561).给定一个2n个整数的数组,你的任务是将这些整数分组为n对 ...
- JetBrains 注册码
C40PF37RR0-eyJsaWNlbnNlSWQiOiJDNDBQRjM3UlIwIiwibGljZW5zZWVOYW1lIjoiemhhbmcgeW9uZyIsImFzc2lnbmVlTmFtZ ...
- 多线程--wait()和notify(),Thread中的等待和唤醒方法
package com.thread.test; public class SimpleMN { final static Object object = new Object(); public s ...
- .NET CORE学习笔记系列(4)——ASP.NET CORE 程序启用SSL
一.什么是SSL? 1.概念: SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数 ...
- Django 【认证系统】auth
本篇内容 介绍Django框架提供的auth 认证系统 方法: 方法名 备注 create_user 创建用户 authenticate 登录验证 login 记录登录状态 logout 退出用户登录 ...
- mybatis中大于等于小于的写法
第一种写法(1): 原符号 < <= > >= & ' "替换符号 < <= > >= & ' " ...
- [LeetCode] 5. 最长回文子串
题目链接:https://leetcode-cn.com/problems/longest-palindromic-substring/ 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你 ...
- Python爬虫-爬取豆瓣电影Top250
#!usr/bin/env python3 # -*- coding:utf-8-*- import requests from bs4 import BeautifulSoup import re ...