题面:
Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her.
After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly n houses, and some of them are connected with directed road. To visit his girlfriend, Jerry needs to start from his house indexed 1 and go along the shortest path to hers, indexed n.
Now Tom wants to block some of the roads so that Jerry has to walk longer to reach his girl’s home, and he found that the cost of blocking a road equals to its length. Now he wants to know the minimum total cost to make Jerry walk longer.
Note, if Jerry can’t reach his girl’s house in the very beginning, the answer is obviously zero. And you don’t need to guarantee that there still exists a way from Jerry’s house to his girl’s after blocking some edges.
题意:
给你一张有向图,现在你需要删除一些边,每次删除的花费是边的权值,使得最短路增大,现在问你最小的花费。
思路: 如果要使得最短路增大,显然是删掉最短路上的一些边。所以我们跑两次最短路算法将最短路的图重构出来,接着套模板跑一遍最小割即可
最小割==最大流
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e4+5;
struct node
{
int v,nxt,w;
}edge2[N],edge[N*2];
bool vis[N];
int u[N],v[N],w[N],cnt,s,t;
int dep[N],cur[N],head[N],head2[N];
ll dis1[N],dis2[N],flow[N*2];
void add2(int u,int v,int w)
{
edge2[cnt].w=w;
edge2[cnt].v=v;
edge2[cnt].nxt=head2[u];
head2[u]=cnt++;
}
void add(int u,int v,int w)
{
edge[cnt].w=w;
edge[cnt].v=v;
edge[cnt].nxt=head[u];
head[u]=cnt;
flow[cnt++]=w;
}
void dij(int s,ll dis[])
{
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > >q;
q.push({0ll,s});
dis[s]=0;
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vis[u])
continue;
vis[u]=1;
for(int i=head2[u];i!=-1;i=edge2[i].nxt)
{
int v=edge2[i].v;
int w=edge2[i].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
q.push({dis[v],v});
}
}
}
}
void init(int n)
{
for(int i=1;i<=n;i++)
{
head[i]=head2[i]=-1;
dis1[i]=dis2[i]=INF;
vis[i]=0;
cur[i]=0;
dep[i]=0;
}
cnt=0;
}
ll dfs(int u,ll cost)
{
if(u==t)
return cost;
for(int i=cur[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].v;
if(dep[v]==dep[u]+1&&flow[i]>0)
{
ll dis=dfs(v,min(flow[i],cost));
if(dis>0)
{
flow[i]-=dis;
flow[i^1]+=dis;
return dis;
}
}
}
return 0;
}
int bfs()
{
queue<int>q;
q.push(1);
memset(dep,0,sizeof dep);
dep[1]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].v;
if(flow[i]>0&&dep[v]==0)
{
dep[v]=dep[u]+1;
q.push(v);
}
}
}
if(dep[t]>0)
return 1;
return 0;
}
ll dicnic()
{
ll ans=0;
while(bfs())
{
ll cost;
memcpy(cur,head,sizeof(head));
while(cost=dfs(1,INF))
ans+=cost;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
init(n);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
add2(u[i],v[i],w[i]);
}
dij(1,dis1);
for(int i=0;i<=n;i++)
{
vis[i]=0;
head2[i]=-1;
}
cnt=0;
for(int i=1;i<=m;i++)
add2(v[i],u[i],w[i]);
dij(n,dis2);
cnt=0;
t=n;
for(int i=1;i<=m;i++)
{
if(dis1[u[i]]+w[i]+dis2[v[i]]==dis1[n])
{
add(u[i],v[i],w[i]);
add(v[i],u[i],0);
}
}
printf("%lld\n",dicnic());
}
return 0;
}