BZOJ3040: 最短路(road)
https://www.lydsy.com/JudgeOnline/problem.php?id=3040
题目已经给出是最短路,所以我们可以用dijkstra(关于spfa它死了),但是看一眼这道题的数据范围,普通的dij肯定是会超时的。所以要优化(我不会优化所以直接借鉴了hzwer学长的代码)这里用的是平板电视优化QAQ
#include<cstdio> #include<queue> #include<algorithm> #include<iostream> #include<cmath> #include<ext/pb_ds/priority_queue.hpp> #define llinf 9000000000000000000LL typedef long long ll; #define pa pair<ll,int> using namespace std; using namespace __gnu_pbds; typedef __gnu_pbds::priority_queue<pa,greater<pa>,pairing_heap_tag > heap; int n, m; heap::point_iterator id[1000005]; 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; } struct edge { int to, nxt, from; } e[10000005]; int tot; int head[1000005]; ll dis[1000005]; void add(int x, int y, int z) { e[++tot].nxt = head[x]; e[tot].to = y; e[tot].from = z; head[x] = tot; } void dijkstra() { heap q; for(int i = 1; i <= n; i++) dis[i] = llinf; dis[1] = 0; id[1] = q.push(make_pair(0,1)); while(!q.empty()) { int x = q.top().second; q.pop(); for(int i = head[x]; i; i = e[i].nxt) if(dis[e[i].to] > dis[x] + e[i].from) { dis[e[i].to] = dis[x] + e[i].from; if(id[e[i].to] != 0) q.modify(id[e[i].to],make_pair(dis[e[i].to],e[i].to)); else id[e[i].to] = q.push(make_pair(dis[e[i].to],e[i].to)); } } } int T, rxa, rxc, rya, ryc, rp, x, y, z; int main() { n = read(), m = read(); T = read(), rxa = read(), rxc = read(), rya = read(), ryc = read(), rp = read(); int a, b; for(int i = 1; i <= T; i++) { x = ((ll)x * rxa + rxc) % rp; y = ((ll)y * rya + ryc) % rp; a = min(x % n + 1,y % n + 1); b = max(y % n + 1,y % n + 1); add(a,b,1e8 - 100 * a); } for(int i = 1; i <= m - T; i++) { x = read(), y = read(), z = read(); add(x,y,z); } dijkstra(); printf("%lld", dis[n]); return 0; }