[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1880
[算法]
最短路 + 动态规划
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 1510 const int INF = 2e9; struct edge { int to , w , nxt; } e[MAXN * MAXN]; int tot , n , m , sp1 , sp2 , X1 , X2 , Y1 , Y2; int head[MAXN] , dist1[MAXN] , dist2[MAXN] , dist3[MAXN] , dist4[MAXN] , f[MAXN] , visited[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } inline void addedge(int u ,int v , int w) { tot++; e[tot] = (edge){v , w , head[u]}; head[u] = tot; } inline void dijkstra(int *dist , int s) { priority_queue< pair<int , int> , vector< pair<int , int> > , greater< pair<int , int> > > q; for (int i = 1; i <= n; i++) { dist[i] = INF; visited[i] = false; } dist[s] = 0; q.push(make_pair(0 , s)); while (!q.empty()) { int cur = q.top().second; q.pop(); if (visited[cur]) continue; visited[cur] = true; for (int i = head[cur]; i; i = e[i].nxt) { int v = e[i].to , w = e[i].w; if (dist[cur] + w < dist[v]) { dist[v] = dist[cur] + w; q.push(make_pair(dist[v] , v)); } } } } inline int dp(int u) { if (f[u] != -1) return f[u]; if (u == Y1 || u == Y2) return f[u] = 0; f[u] = 0; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to , w = e[i].w; if (dist1[u] + w + dist3[v] == sp1 && dist2[u] + w + dist4[v] == sp2) chkmax(f[u] , dp(v) + w); } return f[u]; } int main() { read(n); read(m); read(X1); read(Y1); read(X2); read(Y2); for (int i = 1; i <= m; i++) { int u , v , w; read(u); read(v); read(w); addedge(u , v , w); addedge(v , u , w); } dijkstra(dist1 , X1); dijkstra(dist2 , X2); dijkstra(dist3 , Y1); dijkstra(dist4 , Y2); sp1 = dist1[Y1] , sp2 = dist2[Y2]; memset(f , 255 , sizeof(f)); for (int i = 1; i <= n; i++) dp(i); int ans = 0; for (int i = 1; i <= n; i++) chkmax(ans , f[i]); dijkstra(dist1 , Y1); dijkstra(dist3 , X1); swap(X1 , Y1); memset(f , 255 , sizeof(f)); for (int i = 1; i <= n; i++) dp(i); for (int i = 1; i <= n; i++) chkmax(ans , f[i]); printf("%d " , ans); return 0; }