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)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
随机推荐
- Spring Mvc Web 配置拦截规则与访问静态资源 (三)
拦截规则配置 1. *.do <!-- Processes application requests --> <servlet> <servlet-name>app ...
- BZOJ4818 序列计数
4818: [Sdoi2017]序列计数 Time Limit: 30 Sec Memory Limit: 128 MB Description Alice想要得到一个长度为n的序列,序列中的数都是 ...
- JQuery 对表格的详细操作
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding=& ...
- JavaScript 计时
http://www.w3school.com.cn/js/js_timing.asp JavaScript 计时事件 通过使用 JavaScript,我们有能力作到在一个设定的时间间隔之后来执行代码 ...
- python中的__getattr__、__getattribute__、__setattr__、__delattr__、__dir__
__getattr__: 属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp12 ...
- Python人工智能之路 - 第一篇 : 你得会点儿Python基础
Python 号称是最接近人工智能的语言,因为它的动态便捷性和灵活的三方扩展,成就了它在人工智能领域的丰碑 走进Python,靠近人工智能 一.编程语言Python的基础 之 "浅入浅出&q ...
- 关于golang的defer的练习
golang的defer怎么说.大意就是在函数return后.函数关闭前.按照filo的顺序来执行的关键字 上代码: package main import ( "fmt" ) f ...
- IOS使用批处理打包
一.注意 1.允许xcode访问钥匙串 首先使用xcode提供的打包工具打包,看到如下提示后,输入用户密码后点击“始终允许”后再次打包即可. 选择“Generic IOS Device”然后单击Pro ...
- 3.操作jQuery集合《jquery实战》
3.1 创建HTML元素 使用 jquery 创建动态元素是相当容易的.可以通过 $() 函数包含一个 HTML 标签的字符串来创建. $('<div>Hello</div>' ...
- norbert-构建服务器集群感知的 Java 应用程序
http://www.ibm.com/developerworks/cn/java/j-zookeeper/index.html