标签:
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 178 Accepted Submission(s): 44
#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <cstdlib> #include <functional> #include <algorithm> using namespace std; const int MAXN = 2e5+10; typedef long long LL; typedef pair <LL, int>pli; const LL inf = 1LL << 60; LL L[MAXN], R[MAXN], cost[MAXN], dis[MAXN]; int dsu[MAXN]; void init (){ for (int i = 0 ; i< MAXN; i++){ dis[i] = inf; dsu[i] = i; } } int find (int s){ return dsu[s] = (dsu[s] == s ? s : find(dsu[s])); } int main() { int n, T; scanf ("%d", &T); while (T--) { init(); scanf ("%d", &n); for (int i = 1; i <= n; i++) { scanf ("%I64d", L+i); } for (int i = 1; i <= n; i++) { scanf ("%I64d", R+i); } for (int i = 1; i <= n; i++) { scanf ("%I64d", cost+i); } init(); dis[1] = cost[1]; priority_queue<pli, vector<pli>, greater<pli> >Q; Q.push(make_pair(0LL, 1)); while (!Q.empty()){ pli tmp = Q.top(); Q.pop(); int u = tmp.second; for (int i = -1; i <= 1; i += 2){ int lf = L[u] * i + u; int rg = R[u] * i + u; if (lf > rg){ swap(lf, rg); } lf = max(lf, 1); lf = min(lf, n + 1); if (lf > rg){ continue; } for (int v = lf; ; v ++){ v = find(v); if (v <= 0 || v > n || v > rg){ break; } if (dis[v] > dis[u] + cost[v]){ dis[v] = dis[u] + cost[v]; Q.push(make_pair(dis[v], v)); } dsu[find(v)] = find(v + 1); } } } printf("0"); for (int i = 2; i <= n; i++) { printf(" %I64d", dis[i] != inf ? dis[i] - cost[i] : -1); } printf("\n"); } return 0; }
(2015多校第6场)HDU5361--In Touch (Dijkstra应用)
标签:
原文地址:http://www.cnblogs.com/oneshot/p/4709207.html