标签:
对于30%的数据,N ≤ 100;
对于60%的数据,N ≤ 1000;
对于100%的数据,N ≤ 1500,输入数据保证没有重边和自环。
#include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #include <queue> using namespace std; const int maxn = 1510,inf = 1e9; struct node { int u, v, w; }; int n, m, x1, y1, x2, y2,ans,rudu[maxn],d[maxn],dx1[maxn],dx2[maxn],dy1[maxn],dy2[maxn],vis[maxn]; vector <int> g1[maxn]; vector <node> e, g2[maxn]; void add1(int u, int v, int w) { node temp; temp.u = u; temp.v = v; temp.w = w; e.push_back(temp); g1[u].push_back((int)e.size() - 1); } void add2(int u, int v, int w) { node temp; temp.u = u; temp.v = v; temp.w = w; g2[u].push_back(temp); rudu[v]++; } void spfa(int s, int *d) { memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) d[i] = inf; queue <int> q; d[s] = 0; vis[s] = 1; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = 0; i < g1[u].size(); i++) { node temp = e[g1[u][i]]; int v = temp.v; if (d[v] > d[u] + temp.w) { d[v] = d[u] + temp.w; if (!vis[v]) { vis[v] = 1; q.push(v); } } } } } void topo() { queue <int> q; for (int i = 1; i <= n; i++) if (!rudu[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < g2[u].size(); i++) { int v = g2[u][i].v; if (d[v] < d[u] + g2[u][i].w) { d[v] = d[u] + g2[u][i].w; ans = max(ans, d[v]); } if (!(--rudu[v])) q.push(v); } } } int main() { scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add1(u, v, w); add1(v, u, w); } spfa(x1, dx1); spfa(x2, dx2); spfa(y1, dy1); spfa(y2, dy2); for (int i = 0; i < e.size(); i++) { int u = e[i].u, v = e[i].v, w = e[i].w; int len1 = min(dx1[u], dx1[v]) + min(dy1[u], dy1[v]) + w; int len2 = min(dx2[u], dx2[v]) + min(dy2[u], dy2[v]) + w; if (len1 == dx1[y1] && len2 == dx2[y2]) { if (dx1[u] < dx1[v]) add2(u, v, w); else add2(v, u, w); } } topo(); printf("%d", ans); return 0; }
标签:
原文地址:http://www.cnblogs.com/zbtrs/p/5815958.html