From ccafa64035bc6bf7d57b17f0be070a21d7b4233b Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Wed, 27 Apr 2016 20:40:15 +0800 Subject: [PATCH] From http://blog.csdn.net/qq_33765907/article/details/51228610 --- ZOJ/3946_csdn.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ZOJ/3946_csdn.cpp diff --git a/ZOJ/3946_csdn.cpp b/ZOJ/3946_csdn.cpp new file mode 100644 index 0000000..ff88347 --- /dev/null +++ b/ZOJ/3946_csdn.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; +typedef long long LL; +typedef pair PII; + +const int MAXN = 100000 + 10; +vector G[MAXN]; +int X[MAXN], Y[MAXN], D[MAXN], C[MAXN]; +LL ds[MAXN], ms[MAXN]; +int n, m; + +void run() { + cin >> n >> m; + for (int i = 0; i < n; ++i) { + G[i].clear(); + ms[i] = ds[i] = 1ll << 60; + } + for (int i = 0; i < m; ++i) { + cin >> X[i] >> Y[i] >> D[i] >> C[i]; + G[X[i]].push_back(PII(Y[i], i)); + G[Y[i]].push_back(PII(X[i], i)); + } + ds[0] = ms[0] = 0; + queue Q; Q.push(0); + vector vs(n); + while (!Q.empty()) { + int u = Q.front(); Q.pop(); vs[u] = 0; + for (auto &x: G[u]) { + int v = x.first, w = D[x.second]; + if (ds[v] > ds[u] + w) { + ds[v] = ds[u] + w; + if (!vs[v]) vs[v] = 1, Q.push(v); + } + } + } + for (int i = 0; i < m; ++i) { + if (ds[X[i]] + D[i] == ds[Y[i]]) { + ms[Y[i]] = min(ms[Y[i]], (LL)C[i]); + } + if (ds[Y[i]] + D[i] == ds[X[i]]) { + ms[X[i]] = min(ms[X[i]], (LL)C[i]); + } + } + LL SD = 0, SC = 0; + for (int i = 0; i < n; ++i) SD += ds[i], SC += ms[i]; + cout << SD << " " << SC << endl; +} + +int main() { + int T; cin >> T; + for (int cas = 1; cas <= T; ++cas) run(); + return 0; +}