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)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
随机推荐
- 流媒体技术学习笔记之(十二)Linux(Ubuntu)环境运行EasyDarwin
Debug问题??? ./easydarwin -c ./easydarwin.xml & //这样的话是80端口 ./easydarwin -c ./easydarwin.xml -d // ...
- Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang
Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang The hugely popular Liberty Mut ...
- 第10月第4天 Mac g++ sfml opendir
1. g++ OpenGL.cpp -I/Users/temp/Downloads/SFML-2.4.2-osx-clang/include -L/usr/local/lib -framework O ...
- JAVA不可变类与可变类、值传递与引用传递深入理解
一个由try...catch...finally引出的思考,在前面已经初步了解过不可变与可变.值传递与引用传递,在这里再次深入理解. 1.先看下面一个try..catch..finally的例子: P ...
- TensorFlow 从零到helloWorld
目录 1.git安装与使用 1.1 git安装 1.2 修改git bash默认路径 1.3 git常用操作 2.环境搭建 2.1 tensorflow安装 2.2 CUDA安装 2.3 ...
- MFC小型工具通用界面框架CLIST控件+右键菜单功能
MFC-小型工具通用界面框架 0x1 场景 由于工作需要我会写代码开发工具给客户或者同事用.代码都能实现,但写个黑乎乎的命令行工具给别人用确实显得不够专业,用别人写好的成型工具又担心有后门. 所以掌握 ...
- python3之模块psutil系统性能信息
psutil是个跨平台库,能够轻松实现获取系统运行的进程和系统利用率,包括CPU.内存.磁盘.网络等信息. 它主要应用于信息监控,分析和限制系统资源及进程的管理.它实现了同等命令命令行工具提供的功能, ...
- CentOS安装SVN客户端(rpm)
http://mirrors.163.com/centos/6/os/x86_64/Packages/ 1.检查是已经安装了svn: rpm -qa subversion subversion-1.7 ...
- mac ssh 自动登陆设置
1.首先找到.ssh目录 一般在用户名目录下. ls -a查看 如果没有就重新创建一个 chennan@bogon :mkdir .ssh chennan@bogon 查看当前的 bogon:.ssh ...
- Project Euler Problem2
Even Fibonacci numbers Problem 2 Each new term in the Fibonacci sequence is generated by adding the ...