5818. 【NOIP提高A组模拟2018.8.15】 做运动
(File IO): input:running.in output:running.out
Time Limits: 2000 ms Memory Limits: 524288 KB Detailed Limits
Goto ProblemSet做法:将边按温度从小到大排序,用并查集加边查询S和T的联通情况,当S和T恰好联通时(剩下的温度相同的也要加进去),跑一遍spfa就可以了
代码如下:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> #define N 1000007 #define NU 17442269567978 #define LL long long using namespace std; LL n, m, S, T, tot, ls[N], Ans, cnt; LL dis[N]; int f[N]; struct edge { LL to, next; LL w; }e[N * 2]; struct arr { int x, y, t, w; }E[N]; queue<LL> q; bool v[N * 2]; inline int Read() { int s = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s; } inline void Add(int x, int y, LL z) { e[++tot].to = y; e[tot].w = z; e[tot].next = ls[x]; ls[x] = tot; } int Find(int x) { if (f[x] == 0) return x; f[x] = Find(f[x]); return f[x]; } void Work() { for (int i = 1; i <= m; i++) { int u = Find(E[i].x), v = Find(E[i].y); if (u != v) f[u] = v; if (Find(S) == Find(T)) { cnt = i; int k = E[i].t; for (int j = i + 1; j <= m; j++) { if (E[j].t > k) break; cnt = j; } Ans = k; return; } } } void Spfa() { for (int i = 1; i <= n; i++) dis[i] = NU; memset(v, 0, sizeof(v)); dis[S] = 0; v[S] = 1; q.push(S); for (; !q.empty();) { int now = q.front(); q.pop(); for (int i = ls[now]; i; i = e[i].next) { if (dis[now] + e[i].w < dis[e[i].to]) { dis[e[i].to] = dis[now] + e[i].w; if (!v[e[i].to]) { v[e[i].to] = 1; q.push(e[i].to); } } } v[now] = 0; } } int Cmp(arr x, arr y) { return x.t < y.t; } void Init() { for (int i = 1; i <= cnt; i++) { Add(E[i].x, E[i].y, (LL)(E[i].t * E[i].w)); Add(E[i].y, E[i].x, (LL)(E[i].t * E[i].w)); } } int main() { freopen("running.in", "r", stdin); freopen("running.out", "w", stdout); n = Read(), m = Read(); for (int i = 1; i <= m; i++) E[i].x = Read(), E[i].y = Read(), E[i].t = Read(), E[i].w = Read(); sort(E + 1, E + m + 1, Cmp); S = Read(); T = Read(); Work(); Init(); Spfa(); printf("%d", Ans); printf(" %lld", dis[T]); }