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的最短路,使得 ...
随机推荐
- 001.ActiveMQ概述
1. 概念 ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现的消息中间件(Message Oriented Middleware ...
- Codeforces 845A. Chess Tourney 思路:简单逻辑题
题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力. 实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢. 叫你把这2*n个选手分成2个有n个选手的队伍. ...
- vb常用的内部函数(二):字符串函数
len(string):计算字符串长度函数.返回字符串string中字符的个数.一个汉字为一个字符,空格也为一个字符,空字符串的长度为0. Ltrim(string).Rtrim(string).Tr ...
- pandas 7 合并 merge 水平合并,数据会变宽
pd.merge( df1, df2, on=['key1', 'key2'], left_index=True, right_index=True, how=['left', 'right', 'o ...
- centos7.3安装php7.0
需求:在Centos7.3下搭建LNMP环境 文章转载自:http://blog.csdn.net/wszll_alex/article/details/76285324 作者:狂热森林 . 关闭防火 ...
- Qt5.7新特性
简述 Qt5.7发布了,新特性如下. 简述 新特性 C11 Support Required from the compiler New Features within existing module ...
- Fedora 17 安裝完全指南
Fedora 17 关闭U盘自动mount gsettings set org.gnome.desktop.media-handling automount "false" gse ...
- IOS中UIImagePickerController中文界面问题
今天沈阳斌子,写IOS项目遇到一个调用照相机的问题,找到解决方法,高速攻克了拿给PM看,结果PM说程序调用的照相机不是中文的是英文的.必须改成中文.上网找到了方法.试用后好用拿出来和大家分享.方法例如 ...
- leetcode 刷题之路 66 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- C++ 何时使用动态分配(即使用newkeyword)?何时使用指针?
动态分配 在你的问题里.你用了两种方式创建对象.这两种方式基本的不同在于对象的存储时间. 当运行Object myObject;这句代码时.它作为自己主动变量被创建,这意味着当对象出了作用域时也会自己 ...