题目链接:hdu_5889_Barricade
题意:
有n个点,m条边,每个边的长度都为1,每个边有一个消耗w,如果要阻断这条路,那么就会消耗w,现在让你阻断点1到点n的所有最短路,问你最小的消耗是多少
题解:
先用dij算出最短路,然后再枚举每一条边,如果dis[u]+1=dis[v],那么久在网络流里加一条u到v的边,消耗为w,
最后用板子跑一下最大流就行了
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std; #define MAXN 20010//边数
#define inf 10000000
int t,n,m,x,y,c,egg[MAXN][];
int v[MAXN], w[MAXN], nxt[MAXN], gg[MAXN], ed, dd[MAXN];//n为点数,d为起点到每点的最短路程,初始化ed为0,g初始化为0
void adg(int x, int y, int z) { v[++ed] = y; w[ed] = z; nxt[ed] = gg[x]; gg[x] = ed;}
typedef pair<int, int>P;
priority_queue<P, vector<P>, greater<P> > Q;
void dijkstra(int S) {
int i, x;
for (i = ; i <= n; i++)dd[i] = inf; Q.push(P(dd[S] = , S));
while (!Q.empty()) {
P t = Q.top(); Q.pop();
if (dd[x = t.second] < t.first)continue;
for (i = gg[x]; i; i = nxt[i])if (dd[x] + w[i] < dd[v[i]])Q.push(P(dd[v[i]] = dd[x] + w[i], v[i]));
}
} const int N=,M=;
struct edge{int t,f;edge*nxt,*pair;}*g[N],*d[N],pool[M],*cur=pool;
struct ISAP{
int n,m,i,S,T,h[N],gap[N],maxflow;
void init(int ss,int tt){for(S=ss,T=tt,cur=pool,i=;i<=T;i++)g[i]=d[i]=NULL,h[i]=gap[i]=;}
void add(int s,int t,int f){
edge*p=cur++;p->t=t,p->f=f,p->nxt=g[s],g[s]=p;
p=cur++,p->t=s,p->f=,p->nxt=g[t],g[t]=p;
g[s]->pair=g[t],g[t]->pair=g[s];
}
int sap(int v,int flow){
if(v==T)return flow;
int rec=;
for(edge*p=d[v];p;p=p->nxt)if(h[v]==h[p->t]+&&p->f){
int ret=sap(p->t,min(flow-rec,p->f));
p->f-=ret;p->pair->f+=ret;d[v]=p;
if((rec+=ret)==flow)return flow;
}
if(!(--gap[h[v]]))h[S]=T;
gap[++h[v]]++;d[v]=g[v];
return rec;
}
int get_ans(){
for(gap[maxflow=]=T,i=;i<=T;i++)d[i]=g[i];
while(h[S]<T)maxflow+=sap(S,inf);
return maxflow;
}
}G; int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
G.init(,n);
memset(gg,,sizeof(gg)),ed=;
F(i,,m)
{
scanf("%d%d%d",&x,&y,&c),adg(x,y,),adg(y,x,);
egg[i][]=x,egg[i][]=y,egg[i][]=c;
}
dijkstra();
F(i,,m)
{
if(dd[egg[i][]]+==dd[egg[i][]])G.add(egg[i][],egg[i][],egg[i][]);
if(dd[egg[i][]]+==dd[egg[i][]])G.add(egg[i][],egg[i][],egg[i][]);
}
printf("%d\n",G.get_ans());
}
}