先跑出最短路,然后要求把最短路切断,并且花费最小,那么就是最小割
套个板子就过了,水题
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 100005
#define MAXN 1000005
#define maxnode 15
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-4;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
const int N = 1005;
const int M = 10005;
struct Isap{
int tot;
int d[N],pre[N],cur[N],gap[N];
int head[N];
struct Edge{
int v,next;
int cap,flow;
}edge[M*2];
void init(){
mem(head,-1);
tot=0;
}
void add_edge(int a, int b, int c){
edge[tot]=(Edge){b,head[a],c,0};
head[a]=tot++;
}
void add(int a,int b, int c){
add_edge(a,b,c);
add_edge(b,a,0);
}
void set_d(int t){
queue<int> q;
mem(d,-1);
mem(gap,0);
d[t]=0;
q.push(t);
while(!q.empty()) {
int u=q.front();q.pop();
++gap[d[u]];
for(int i=head[u];i!=-1;i=edge[i].next) {
int v=edge[i].v;
if(d[v]==-1) {
d[v]=d[u]+1;
q.push(v);
}
}
}
}
int sap(int s,int t,int num) {
set_d(t);
int ans=0,u=s;
int flow=INF;
memcpy(cur,head,sizeof(head));
while(d[s]<num){
int &i=cur[u];
for(;i!=-1;i=edge[i].next) {
int v=edge[i].v;
if(edge[i].cap>edge[i].flow&&d[u]==d[v]+1) {
u=v;
pre[v]=i;
flow=min(flow,edge[i].cap-edge[i].flow);
if(u==t){
while(u!=s){
int j=pre[u];
edge[j].flow+=flow;
edge[j^1].flow-=flow;
u=edge[j^1].v;
}
ans+=flow;
flow=INF;
}
break;
}
}
if(i==-1) {
if(--gap[d[u]]==0) break;
int dmin=num-1;
cur[u]=head[u];
for(int j=head[u];j!=-1;j=edge[j].next)
if(edge[j].cap>edge[j].flow)
dmin=min(dmin,d[edge[j].v]);
d[u]=dmin+1;
++gap[d[u]];
if(u!=s) u=edge[pre[u]^1].v;
}
}
return ans;
}
}Sap;
//调用方式:
//Sap.init(); //建边前调用
//Sap.add(u, v, c); //在u->v之间建一条容量为c的边
//Sap.sap(s, t, n); //s为源点,t为汇点,n为点的数量
vector<pii> v[MAX];
int dis[MAX];
int vis[MAX];
int n;
void spfa(int s,int t){
mem(dis,INF);
mem(vis,0);
queue<int> q;
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=0;
for(int i=0;i<v[u].size();i++){
pii tmp=v[u][i];
int vv=tmp.first;
if(dis[vv]>dis[u]+1){
dis[vv]=dis[u]+1;
if(!vis[vv]){
vis[vv]=1;
q.push(vv);
}
}
}
}
Sap.init();
for(int i=1;i<=n;i++){
for(int j=0;j<v[i].size();j++){
pii tmp=v[i][j];
int vv=tmp.first;
if(dis[vv]==dis[i]+1){
Sap.add(i,vv,tmp.second);
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--){
int m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) v[i].clear();
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
v[a].push_back(mk(b,c));
v[b].push_back(mk(a,c));
}
spfa(1,n);
cout<<Sap.sap(1,n,n)<<endl;
}
return 0;
}