Path
题意:求最少的花费使得从1到n的距离变长。
思路:跑两次最短路,如果d[u]+d[v]+w==d[n]那么在最短路上,用这些边建图,跑最大流即可。
代码来自:https://blog.csdn.net/qq_41730082/article/details/96874510
#include "bits/stdc++.h"
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxN = 1e4 + 7;
int N, M, head[maxN], cnt, top[maxN], tot, cur[maxN], TT[maxN], Index;
struct Eddge
{
int nex, u, to;
ll val;
Eddge(int a=-1, int me = 0, int b=0, ll c=0):nex(a), u(me), to(b), val(c) {}
}edge[maxN<<1], re_ed[maxN<<1], path[maxN<<1];
void re_E(int u, int v, ll w)
{
re_ed[Index] = Eddge(TT[u], u, v, w);
TT[u] = Index++;
}
void addEddge(int u, int v, ll w)
{
edge[cnt] = Eddge(head[u], u, v, w);
head[u] = cnt++;
}
void addPath(int u, int v, ll w)
{
path[tot] = Eddge(top[u], u, v, w);
top[u] = tot++;
}
void _add(int u, int v, ll w) { addPath(u, v, w); addPath(v, u, 0); }
ll dist[maxN], lence[maxN];
queue<int> Q;
bool inque[maxN];
bool spfa()
{
for(int i=2; i<=N; i++) dist[i] = INF;
dist[1] = 0;
for(int i=2; i<=N; i++) inque[i] = false;
inque[1] = true;
while(!Q.empty()) Q.pop();
Q.push(1);
while(!Q.empty())
{
int u = Q.front(); Q.pop(); inque[u] = false;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to; ll w = edge[i].val;
if(dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
if(!inque[v])
{
inque[v] = true;
Q.push(v);
}
}
}
}
return dist[N] == INF;
}
void re_spfa()
{
for(int i=1; i<=N; i++) lence[i] = INF;
lence[N] = 0;
for(int i=1; i<=N; i++) inque[i] = false;
inque[N] = true;
while(!Q.empty()) Q.pop();
Q.push(N);
while(!Q.empty())
{
int u = Q.front(); Q.pop(); inque[u] = false;
for(int i=TT[u], v; ~i; i=re_ed[i].nex)
{
v = re_ed[i].to; ll w = re_ed[i].val;
if(lence[v] > lence[u] + w)
{
lence[v] = lence[u] + w;
if(!inque[v])
{
inque[v] = true;
Q.push(v);
}
}
}
}
}
int deep[maxN];
bool bfs()
{
for(int i=2; i<=N; i++) deep[i] = 0;
deep[1] = 1;
while(!Q.empty()) Q.pop();
Q.push(1);
while (!Q.empty())
{
int u =Q.front(); Q.pop();
for(int i=top[u], v; ~i; i=path[i].nex)
{
v = path[i].to; ll f = path[i].val;
if(f > 0 && !deep[v])
{
deep[v] = deep[u] + 1;
if(v == N)
return 1;
Q.push(v);
}
}
}
return deep[N];
}
ll dfs(int u, ll Flow)
{
if(u == N) return Flow;
for(int &i=cur[u], v; ~i; i=path[i].nex)
{
v = path[i].to; ll f = path[i].val;
if(f > 0 && deep[v] == deep[u] + 1)
{
ll di = dfs(v, min(f, Flow));
if(di > 0)
{
path[i].val -= di;
path[i^1].val += di;
return di;
}
}
}
return 0;
}
ll Dinic()
{
ll ans = 0, tmp = 0;
while(bfs())
{
for(int i=1; i<=N; i++) cur[i] = top[i];
while((tmp = dfs(1, INF))) ans += tmp;
}
return ans;
}
void init()
{
for(int i=0; i<=N; i++) head[i] = top[i] = TT[i] = -1;
cnt = tot = Index = 0;
}
int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &M);
init();
for(int i=1, u, v; i<=M; i++)
{
ll w;
scanf("%d%d%lld", &u, &v, &w);
if(u == v) continue;
addEddge(u, v, w);
re_E(v, u, w);
}
if(spfa())
{
printf("0\n");
continue;
}
ll minn = dist[N];
re_spfa();
for(int i=0, u, v; i<cnt; i++)
{
u = edge[i].u; v = edge[i].to;
if(dist[u] + lence[v] + edge[i].val == minn)
{
_add(u, v, edge[i].val);
}
}
printf("%lld\n", Dinic());
}
}