[USACO11DEC]RoadBlock S / [USACO14FEB]Roadblock G/S - 洛谷
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
int n,m,a[105][105],pre[105],t,dis[105],ans1,ans2;
bool vis[105];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= m;i ++){
int x,y,z;
cin >> x >> y >> z;
a[x][y] = z;
a[y][x] = z;
}
for(int i = 1;i <= n;i ++){
dis[i] = INF;
pre[i] = 0;
}
t = 1;
pre[1] = 0;
vis[1] = 1;
dis[1] = 0;
for(int i = 1;i < n;i++){
for(int j = 1;j <= n;j ++)
if(a[t][j] > 0 && dis[t]+a[t][j]<dis[j]){
dis[j] = dis[t] + a[t][j];
pre[j] = t;
}
int x = INF;
for(int j = 1;j <= n;j ++){
if(dis[j] < x && !vis[j]){
x = dis[j];
t = j;
}
}
vis[t] = 1;
}
ans1 = dis[n];
// for(int i = 1;i <= n;i ++)
// cout << dis[i] <<" ";
// cout << endl;
int p = n;
while(pre[p]!= 0){
a[p][pre[p]] *= 2;
a[pre[p]][p] *= 2;
memset(vis,0,sizeof(vis));
for(int i = 1;i <= n;i ++)
dis[i] = INF;
t = 1;
vis[1] = 1;
dis[1] = 0;
for(int i = 1;i < n;i++){
for(int j = 1;j <= n;j ++)
if(a[t][j] > 0 && dis[t]+a[t][j]<dis[j]){
dis[j] = dis[t] + a[t][j];
}
int x = INF;
for(int j = 1;j <= n;j ++){
if(dis[j] < x && !vis[j]){
x = dis[j];
t = j;
}
}
vis[t] = 1;
}
// for(int i = 1;i <= n;i ++)
// cout << dis[i] << " ";
// cout << endl;
ans2 = max(ans2,dis[n]);
//cout << ans2 << endl;
a[p][pre[p]]/=2;//类似dfs回溯
a[pre[p]][p]/=2;
p=pre[p];
}
cout << ans2 - ans1 << endl;
return 0;
}