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;
}
上一篇:samba后台进程及安全模式简介


下一篇:iOS开发--CornerStone上传静态库(.a文件)