最短路 西北大学2019年春季校赛 ( 重现赛 ) 房间迷宫

编程入门 行业动态 更新时间:2024-10-10 04:19:54

最短路 西北大学2019年春季校赛 ( 重现赛 ) 房间<a href=https://www.elefans.com/category/jswz/34/1769354.html style=迷宫"/>

最短路 西北大学2019年春季校赛 ( 重现赛 ) 房间迷宫

题目:=1461(密码:jwjtxdy)

学习一下 求一个数的约束 复杂度n*logn

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <iostream>
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
struct heapnode
{int u;ll d;heapnode(int u=0,ll d=0):u(u),d(d){}bool operator<(const heapnode&a)const{return a.d < d;}
};
vector<int>G[maxn];
vector<int>num[maxn];
ll dis[maxn];
bool vis[maxn];
int a[maxn], b[maxn], c[maxn], n;void dij(int s)
{for (int i = 0; i <= n; i++) dis[i] = inf;dis[s] = 0;memset(vis, 0, sizeof(vis));priority_queue<heapnode>que;que.push(heapnode(s, 0));while(!que.empty()){heapnode x = que.top(); que.pop();int u = x.u;if (vis[u]) continue;vis[u] = 1;for(int i=0;i<G[u].size();i++){int now = G[u][i];if(dis[now]>dis[u]+a[now]){dis[now] = dis[u] + a[now];//printf("www  dis[%d]=%lld dis[%d]=%lld\n", now, dis[now],u,dis[u]);
                que.push(heapnode(now, dis[now]));}}//printf("c[%d]=%d\n",u, c[u]);int len = num[c[u]].size();// printf("%d\n", len);for(int i=0;i<len&&num[c[u]][i]+u<=n;i++){int y = num[c[u]][i] + u;// printf("y=%d\n", y);// printf("a[%d]=%d\n", y, a[y]);if(dis[y]>dis[u]+b[u]+a[y]){dis[y] = dis[u] + b[u] + a[y];// printf("b[%d]=%d a[%d]=%d\n", u, b[u], y, a[y]);// printf("dis[%d]=%lld dis[%d]=%lld\n", y, dis[y],u,dis[u]);
                que.push(heapnode(y, dis[y]));}}}
}int main()
{for (int i = 1; i < maxn; ++i) {for (int j = i; j < maxn; j += i) {num[j].push_back(i);//先把每个数的因子有哪些打个表,由调和级数可知复杂度为o(nlog n)
        }}int v;scanf("%d", &n);for(int i=1;i<=n;i++){scanf("%d%d%d%d", &a[i], &b[i], &c[i], &v);G[i].push_back(v);}dij(1);if (dis[n] >= inf) printf("-1\n");else printf("%lld\n", dis[n]+a[1]);return 0;
}

 

转载于:.html

更多推荐

最短路 西北大学2019年春季校赛 ( 重现赛 ) 房间迷宫

本文发布于:2024-02-06 23:19:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迷宫   春季   西北大学   房间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!