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)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
随机推荐
- bzoj千题计划198:bzoj1084: [SCOI2005]最大子矩阵
http://www.lydsy.com/JudgeOnline/problem.php?id=1084 m=1: dp[i][j] 前i个数,选了j个矩阵的最大和 第i个不选:由dp[i-1][j] ...
- History of Monte Carlo Methods - Part 1
History of Monte Carlo Methods - Part 1 Some time ago in June 2013 I gave a lab tutorial on Monte Ca ...
- day63_SpringMVC学习笔记_01
1.JAVAEE体系结构 JAVAEE体系结构图如下所示: 2.什么是springmvc? 什么是mvc? Model1 Model2 SpringMVC是什么? SpringMVC是一个web层mv ...
- [转载]使用 NuGet 管理项目库
原文:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 Mi ...
- 20155310 2016-2017-2 《Java程序设计》第五周学习总结
20155310 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 •收集对象的行为,像是新增对象的add()方法.移除对象的remove()方法等,都是定义在 ...
- MySQL-视图View
视图:一个非真实存在的,囊括复杂查询在内的表,也可以理解成,视图就是一个查询之后的结果. 补充一个概念:临时表查询(子查询):就是在查询语句中再次嵌套一个查询,并将嵌套中的查询设定别名 SELECT ...
- mysql5.7执行sql语句报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonagg
mysql5.7执行sql语句报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonagg ...
- css-通过hover实现鼠标进过背景色变化
通过hover实现鼠标进过背景色变化 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- idea导入maven项目,包没有自动下载
解决办法: 先把这里面的全部删掉 然后在pom.xml里面右键 这样就会重新下载包
- ElastAlert告警
ElastAlert告警 https://blog.csdn.net/qq_38369069/article/details/80842432