【bzoj3040】最短路(road)

来源:互联网 发布:淘宝苏宁易购投诉 编辑:程序博客网 时间:2024/06/07 10:55

3040: 最短路(road)

Time Limit: 60 Sec  Memory Limit: 200 MB
Submit: 2907  Solved: 982
[Submit][Status][Discuss]

Description

N个点,M条边的有向图,求点1到点N的最短路(保证存在)。
1<=N<=1000000,1<=M<=10000000

Input


第一行两个整数N、M,表示点数和边数。
第二行六个整数T、rxa、rxc、rya、ryc、rp。

前T条边采用如下方式生成:
1.初始化x=y=z=0。
2.重复以下过程T次:
x=(x*rxa+rxc)%rp;
y=(y*rya+ryc)%rp;
a=min(x%n+1,y%n+1);
b=max(y%n+1,y%n+1);
则有一条从a到b的,长度为1e8-100*a的有向边。

后M-T条边采用读入方式:
接下来M-T行每行三个整数x,y,z,表示一条从x到y长度为z的有向边。

1<=x,y<=N,0<z,rxa,rxc,rya,ryc,rp<2^31

Output


一个整数,表示1~N的最短路。

Sample Input

3 3
0 1 2 3 5 7
1 2 1
1 3 3
2 3 1

Sample Output

2

HINT

【注释】

请采用高效的堆来优化Dijkstra算法。


Source

WC2013营员交流-lydrainbowcat

[Submit][Status][Discuss]





第一次用pb_ds库,超级激动

发现这个库里居然有这么多好东西,那以后又可以偷懒了。。?


这题就是一个dijstra堆优化,但是如果用std里的堆的话,会因为大量冗余元素没能及时删除和修改而光荣TLE


于是我们需要一个可修改堆

于是用到一波pb_ds库里的堆,话说里边堆的种类真的多,这里代码里选择的是配对堆(性能好啊2333)


跪烂各位手打堆大爷%%%%


代码:

#include<cstdio>#include<cmath>#include<vector>#include<cstring>#include<ext/pb_ds/priority_queue.hpp>  using namespace std;using namespace __gnu_pbds;typedef long long LL;const LL INF = 1000000000000000000LL;const int maxn = 1001000;struct data{int to,w; };struct node{LL w; int id;bool operator > (node b) const {return w > b.w;}};typedef __gnu_pbds::priority_queue <node, greater<node>, pairing_heap_tag> heap;heap Q;heap :: point_iterator pos[maxn];vector<data> e[maxn];int n,m;LL dis[maxn];inline LL getint(){LL ret = 0,f = 1;char c = getchar();while (c < '0' || c > '9'){if (c == '-') f = -1;c = getchar();}while (c >= '0' && c <= '9')ret = ret * 10 + c - '0',c = getchar();return ret * f;}inline void init(){n = getint(); m = getint();LL T = getint(),rxa = getint(),rxc = getint(),rya = getint(),ryc = getint(),rp = getint(),x = 0,y = 0,z = 0,a,b;for (int i = 1; i <= T; i++){x=(x*rxa+rxc)%rp; y=(y*rya+ryc)%rp; a=min(x%n+1,y%n+1); b=max(y%n+1,y%n+1);e[a].push_back((data){b,1e8 - 100 * a});}for (int i = 1; i <= m - T; i++){int u = getint(),v = getint();LL w = getint();e[u].push_back((data){v,w});}}inline void dijstra(){for (int i = 1; i <= n; i++) dis[i] = INF;dis[1] = 0;for (int i = 1; i <= n; i++)pos[i] = Q.push((node){dis[i],i});while (!Q.empty()){int u = Q.top().id;Q.pop();for (int i = 0; i < e[u].size(); i++){int v = e[u][i].to; LL w = e[u][i].w;if (dis[v] > dis[u] + w){dis[v] = dis[u] + w;Q.modify(pos[v],(node){dis[v],v});}}}}int main(){init();dijstra();printf("%lld",dis[n]);return 0;}


原创粉丝点击