题目描述:
有n个传送机排成一排,编号从1到n,每个传送机都可以把自己位置的东西传送到距离自己[l, r]距离的位置,并且花费c,问从1号传送机到其他传送机的最小花费是多少
思路:
从第一个点开始更新, 更新过的点从未被更新的点中除去, 因为在Dijkstra里 每次取出的都是最小的distance, 所以更新过的点 后面肯定不需要再次更新。更新一个点后加入堆中, 因为通过这个点可能更新别的点。
PS:不太懂网上并查集优化的,只知道下面这个集合来优化的。下面这种方式需要最短路的赋值方式,没到该点前,先为到该点的花费赋值好。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<ll, int>
const int maxn = 200005;
int l[maxn], r[maxn];
ll cost[maxn], dis[maxn];
pii p[maxn];
set<int> st;
set<pii> vis;
int main()
{int t, n;scanf("%d", &t);while(t--){scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%d", &l[i]);for(int i = 0; i < n; i++) scanf("%d", &r[i]);for(int i = 0; i < n; i++) scanf("%I64d", &cost[i]);st.clear();vis.clear();memset(dis, -1, sizeof(dis));dis[0] = 0;vis.insert(pii(cost[0], 0));for(int i = 1; i < n; i++) st.insert(i);set<int>::iterator it, it2;while(vis.size() > 0){pii tmp = *vis.begin();vis.erase(vis.begin());int u = tmp.second;it = st.lower_bound(u - r[u]);while(it != st.end() && *it <= u - l[u]){int id = *it;dis[id] = tmp.first;vis.insert(pii(tmp.first + cost[id], id));it2 = it++;st.erase(it2);}it = st.lower_bound(u + l[u]);while(it != st.end() && *it <= u + r[u]){int id = *it;dis[id] = tmp.first;vis.insert(pii(tmp.first + cost[id], id));it2 = it++;st.erase(it2);}}for(int i = 0; i < n; i++)printf("%I64d%c", dis[i], i == n-1 ? '\n' : ' ');}return 0;
}