HDU 4889 Scary Path Finding Algorithm
其实这个题是抄的题解啦…… 题解给了一个图,按照那个图模拟一遍大概就能理解了。
题意:
有一段程序,给你一个C值(程序中某常量),让你构造一组数据,使程序输出"doge"
那段代码大概是 SPFA的SLF优化。其实题目的意思是让我们构造一组数据,使得总的出队次数大于C。
数据范围 C<=23,333,333。输出的图中最多有100个点,没有重边、自环、负环。
思路:
SLF: 设队首元素为 i, 队列中要加入节点 j, 在 时加到队首而不是队尾, 否则和普通的 SPFA 一样加到队尾.
这个优化是基于贪心的思想,因为出当前结点的的距离越小,那么他可能更新点就越多,从而达到优化的目的。
因为是贪心,我们可以“欺骗”他一下。
我们可以让距离比较大的结点加入队列,那么他会比较晚出队,但是,他会经过一系列的会更新原来更新过的结点,那么被更新的点会重新入队。那么被更新点原来的更新路径会重新被更新一次。
题解:来自这里

代码: 先在程序中把图建出来,然后在输出图。这样做会简单一些。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = ; int myPow(int d, int n) {
int res = ;
while (n>) {
if (n&) res *= d;
d *= d;
n >>= ;
}
return res;
} vector<pair<int, int> > G[MAXN];
int n, m; void getGraph() {
for (int i = ; i < MAXN; i++)
G[i].clear();
n = ;
for (int i = ; i >= ; i--) {
G[n].push_back(make_pair(n+, ));
G[n].push_back(make_pair(n+, (i!=) ? (-myPow(, i-)) : ));
G[n+].push_back(make_pair(n+, -myPow(, i)));
n += ;
}
m = ;
for (int i = ; i <= n; i++)
m += G[i].size();
} void output() {
printf("%d %d\n", n, m);
for (int i = ; i <= n; i++)
for (int j = ; j < G[i].size(); j++)
printf("%d %d %d\n", i, G[i][j].first, G[i][j].second); } int main() {
#ifdef Phantom01
freopen("HDU4889.txt", "r", stdin);
#endif //Phantom01 getGraph();
int C;
while (scanf("%d", &C)!=EOF) {
output();
} return ;
}
HDU 4889 Scary Path Finding Algorithm的更多相关文章
- HDU4889 Scary Path Finding Algorithm
Fackyyj loves the challenge phase in TwosigmaCrap(TC). One day, he meet a task asking him to find sh ...
- Proof for Floyd-Warshall's Shortest Path Derivation Algorithm Also Demonstrates the Hierarchical Path Construction Process
(THIS BLOG WAS ORIGINALLY WRTITTEN IN CHINESE WITH LINK: http://www.cnblogs.com/waytofall/p/3732920. ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- HDU 5636 Shortest Path 暴力
Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- hdu 3631 Shortest Path
floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...
- hdu 5385 The path
http://acm.hdu.edu.cn/showproblem.php?pid=5385 题意: 给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得 ...
随机推荐
- 关于PageRank的总结
好久不用CSDN,最近想给带的本科生实验课开个期末习题专题页,发现CSDN的博客忽然要绑定之类.只好弃用回博客园写学习总结了.塞翁失马焉知非福. *************************** ...
- Glidar: 一个基于OpenGL的开源实时3D传感器仿真器
1 简介 这篇博文将介绍一个简单易用的3D传感器仿真器,可以用来模拟Lidars,立体视觉,基于时间飞行技术的ToF相机和微软的Kinect实时产生3D点云数据.Glidar仿真器并不是针对特定的某一 ...
- PC比价软件
方法:从百度中搜索比较软件排行,并且根据将搜索时间缩小在一年内.统计前面一页结果网站中的比价软件. 名称 备注 统计 淘淘搜购物助手 淘宝购物比价神器 1 省省比价软件 主流网购商城进行横向比较的智能 ...
- lua_pcall与lua_call之间的区别
lua_pcall与lua_call之间的区别 定义: void lua_call (lua_State *L, int nargs, int nresults); int lua_pcall (lu ...
- Description Resource Path Location Type Cannot change version of project fac(导入maven项目出现红叉问题)
项目现象如下: 这是由于你的 Maven 编译级别是 jdk太低了 解决方法: 1.在eclipse的工程上选择属性,在选择Project Facets里面中选择Dynamic web Module, ...
- 洛谷P4016 负载平衡问题 费用流
这道题还是很好的. 考察了选手对网络流的理解. 首先,任意两个相邻点之间的运货量时没有限制的. 我们可以将相邻点之间的流量建为无限大,单位费用设为 1,代表运输一个货物需耗费一个代价. 由于题目要求最 ...
- React diff机制(介绍虚拟DOM的机制)
https://segmentfault.com/a/1190000004003055
- (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列
Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...
- shell 文件中添加内容
下文所有 1111 , 2222 均为字符串 sed -i '/1111/i\2222' a.txt 在a.txt中找到所有符合1111得 前面加上2222 sed -i '/1111/a\2 ...
- nginx 多级7层代理安装配置
编译安装 yum install zlib-devel -y wget https://nginx.org/download/nginx-1.15.12.tar.gz tar -zxf nginx-1 ...