OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉
Challenge Your Template
题目连接:
http://acm.hust.edu.cn/vjudge/contest/122701#problem/G
Description
ACM International Collegiate Programming Contest (ICPC) is an annual multi-tiered competitive programming competition among the universities of the world. Compared to other programming contests (for example, International Olympiad in Informatics), the ICPC is characterized by a large number of problems, and thus, the templates of all kinds of algorithms show their importance at this moment.
We are sure that you must have prepared the template for solving the Shortest Path Problem (if not, orz!!!). Come on! Just knock the keyboard as your template writes and click on the submit button to win the first place!
However, we are tired of generating test data, which will cost too much of our sleeping time. we would rather tell you the way of generating random data. In more detail, given the number of nodes (N) in the directed graph G=<V,E,W> and the seed of random number generator (Seed), generate G as following (critical procedure only):
C/C++/Java version:
void buildGraph(int N, int Seed) {
int nextRand = Seed;
// initialize random number generator
for (int x = 1; x <= N; x++) {
// generate edges from Node x
int w = x % 10 + 1; // the weight of edges
int d = 10 - w; // the number of edges
for (int i = 1; i <= d; i++) {
addEdge(x, nextRand % N + 1, w);
// add a new edge into G
nextRand = nextRand * 233 % N;
}
addEdge(x, x % N + 1, w);
}
}
Pascal version:
procedure buildGraph(N: longint; Seed: longint);
var
nextRand, x, w, d, i: longint;
begin
nextRand := Seed; // initialize random number generator
for x := 1 to N do // generate edges from Node x
begin
w := x mod 10 + 1; // the weight of edges
d := 10 - w; // the number of edges
for i := 1 to d do
begin
addEdge(x, nextRand mod N + 1, w);
// add a new edge into G
nextRand := nextRand * 233 mod N;
end;
addEdge(x, x mod N + 1, w);
end;
end;
The function addEdge(x, y, w) adds a new directed edge from Node x to Node y with weight w into G. Note that the nodes of G are numbered from 1 to N, and G contains no edge at the beginning. You are required to calculate the length of the shortest path from Node 1 to Node N in G. It is generated that there exists at least one path from Node 1 to Node N in G. Isn’t it an easy job for you?
Input
The first line contains an integer T (1 ≤ T ≤ 5), indicating the number of test cases.
For each test case:
A line contains two integers N (1 ≤ N ≤ 1,000,000) and Seed (1 ≤ Seed ≤ N).
Output
For each test case, output one integer on a single line, indicating the length of the shortest path from Node 1 to Node N in G.
Sample Input
1
3 1
Sample Output
2
Hint
Use <x,y,w>*k to represent that there are k directed edges from Node x to y with weight w.
In the Sample Input, edges of G are:
<1,2,2>5,<1,3,2>4,<2,2,3>4,<2,3,3>4,
❤️,1,4>1,❤️,2,4>3,❤️,3,4>*3.
The figure of G is shown below (same multiple edges only appear once).
题意
给你一个随机造数据的图,数据范围比较大,让你求1-n的最短路
题解:
数据非常随机,所以直接跑迪杰斯特拉就好了,一下就能跑到终点然后break就好了
代码
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 1000000 + 15;
const int inf = 2e9;
struct edge{
int v , nxt , c;
}e[maxn * 10];
struct node{
int x , y ;
node( int x , int y):x(x),y(y){}
friend bool operator < (const node & a, const node & b){return a.y > b.y;}
};
int tot , head[maxn] , N , dp[maxn];
void link( int u , int v , int c ){
e[tot].v=v,e[tot].nxt=head[u],e[tot].c=c,head[u]=tot++;
}
void buildGraph(int N, int Seed) {
int nextRand = Seed;
int cur = 1;
for (int x = 1; x <= N; x++) {
int w = x % 10 + 1;
int d = 10 - w;
for (int i = 1; i <= d; i++) {
link(x, nextRand % N + 1, w);
nextRand = nextRand * 233 % N;
}
link( x , cur + 1 , w );
++ cur;
if( cur >= N ) cur -= N;
}
}
priority_queue<node>Q;
void dj(){
dp[1]=0;
Q.push(node(1,0));
while(!Q.empty()){
node st = Q.top() ; Q.pop();
int x = st.x , y = st.y;
if( y != dp[x] ) continue;
if( x == N ) break;
for(int i = head[x];~i;i=e[i].nxt){
int v = e[i].v , c = e[i].c;
if( c + dp[x] < dp[v] ){
dp[v] = c + dp[x];
Q.push(node(v,dp[v]));
}
}
}
}
int main(int argc,char *argv[]){
int T = read();
while(T--){
N = read() ; int seed = read();
rep(i,1,N)head[i]=-1,dp[i]=inf;
tot = 0;
buildGraph( N , seed );
dj();
pf("%d\n",dp[N]);
}
return 0;
}
OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉的更多相关文章
- 数据结构图之三(最短路径--迪杰斯特拉算法——转载自i=i++
数据结构图之三(最短路径--迪杰斯特拉算法) [1]最短路径 最短路径?别乱想哈,其实就是字面意思,一个带边值的图中从某一个顶点到另外一个顶点的最短路径. 官方定义:对于内网图而言,最短路径是指两 ...
- C#迪杰斯特拉算法
C#迪杰斯特拉算法 网上有许多版本的,自己还是写一个理解点 Dijkstra.cs public class Dijkstra { private List<Node> _nodes; p ...
- C++迪杰斯特拉算法求最短路径
一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
- 【算法杂谈】LJX的迪杰斯特拉算法报告
迪杰斯特拉(di jie qi)算法 这里有一张图: 假设要求从1号节点到5号节点的最短路.那么根据迪杰斯特拉算法的思想,我们先看: 节点1,从节点1出发的一共有3条路,分别是1-6.1-3.1-2. ...
- C# 迪杰斯特拉算法 Dijkstra
什么也不想说,现在直接上封装的方法: using System; using System.Collections.Concurrent; using System.Collections.Gener ...
- 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)
迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...
- 图-最短路径-Dijktra(迪杰斯特拉)算法
1. 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉算法于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始 ...
- 迪杰斯特拉算法——PAT 1003
本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...
- 最短路径之迪杰斯特拉(Dijkstra)算法
迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
随机推荐
- [转载]HTML5 真的会消灭原生App吗?
http://mp.weixin.qq.com/s?__biz=MzA5NjQ4MzkyMw==&mid=201728945&idx=1&sn=8f43b5bb10695efc ...
- AngularJs -- 内置指令
AngularJS提供了一系列内置指令.其中一些指令重载了原生的HTML元素,比如<form>和<a>标签, 当在HTML中使用标签时,并不一定能明确看出是否在使用指令. 其他 ...
- lrc歌词文件格式
一.lrc文件有什么作用 lrc文件就是一个文本文件,用来记录歌曲的歌词信息,使得播放歌曲时能够让歌词与声音同步显示,类似于电影字幕那种效果. 心情很丧时我们会听首歌陶冶一下情操,不知你是否注意过音乐 ...
- 叉积(POJ - 2318 )
题目链接:https://cn.vjudge.net/contest/276358#problem/A 题目大意:给你一个矩阵的左上角和右下角,然后n个竖杠,这n个竖杠将这个矩阵分成n+1个方块,给你 ...
- Git 使用规范流程【转】
转自:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html 作者: 阮一峰 日期: 2015年8月 5日 团队开发中,遵循一个合理.清 ...
- .NET 的 WCF 和 WebService 有什么区别?(转载)
[0]问题: WCF与 Web Service的区别是什么? 和ASP.NET Web Service有什么关系? WCF与ASP.NET Web Service的区别是什么? 这是很多.NET开发人 ...
- 一个无锁消息队列引发的血案(六)——RingQueue(中) 休眠的艺术 [续]
目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...
- 钉钉机器人-实现监控通知功能-python
1. 首先得创建有 一个 钉钉群.(因为只能发群通知) 2. 添加机器人,得到一个url: 3. 开始写Python脚本: # -*- coding: utf-8 -*- ""&q ...
- Tomcat不同版本所对应的Servlet/JSP规范
上午在别人机器上做演示,写好 Servlet居然访问不了.后来回想叻下,觉得应该是Tomcat版本不一致的问题,我用的是Tomcat 7.0建的Project,他们大多数都是用的6.0的版本.回来又仔 ...
- 字符串前面加u、r、b的含义
u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u:但是中文, ...