The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general's castle is located, and the town numbered N is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i-th road requires wi
units of wood. Because of lacking resources, you need to use as less wood as possible.
Input
The first line of input contains an integer t, then t test cases follow.
For each test case, in the first line there are two integers N(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 denoting an edge between u and v of barricade cost w.
Output
For each test cases, output the minimum wood cost.
Sample Input
1
4 4
1 2 1
2 4 2
3 1 3
4 3 4
Sample Output
4
题目就是让求最短路基础上的最小割,那我们跑一遍Dijstra,把所有最短路上的边取出来,重新建图,跑一遍最大流即可。
(PS:代码太不稳了,不稳就多写几遍。)
#include
#define ll long long
using namespace std;
const int N = 1e3+10;
const int M = 2e4+10;
const int inf = 0x3f3f3f3f;
struct node
{
int to,ca,next;
}g[M];
struct edge
{
int u,v,w;
edge(){}
edge(int u,int v,int w):u(u),v(v),w(w){}
};
struct Node
{
int to,dis;
Node(){}
Node(int to,int dis):to(to),dis(dis){}
friend bool operator <(Node a,Node b)
{
return a.dis>b.dis;
}
};
int head[N],cur[N],dis[N],deep[N],cnt,n,m;
bool vis[N];
vector ve[N];
void Init()
{
cnt=0;
for(int i=1;i<=n;i++)
head[i]=-1,ve[i].clear();
return ;
}
void add(int u,int v,int w)
{
g[cnt].to=v;
g[cnt].ca=w;
g[cnt].next=head[u];
head[u]=cnt++;
return ;
}
//Dijstra从源点到汇点跑最短路
void Dijs(int s,int t)
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue q;
Node now;
int u,v;
dis[s]=0;
q.push(Node(s,0));
while(!q.empty())
{
now=q.top();
q.pop();
u=now.to;
//cout<dis[u]+1)
{
dis[v]=dis[u]+1;
q.push(Node(v,dis[v]));
ve[v].clear();
ve[v].push_back(edge(u,v,g[i].ca));
}
else if(dis[v]==dis[u]+1)
{
ve[v].push_back(edge(u,v,g[i].ca));
}
}
}
return ;
}
//用所有最短路的边建图
void Build()
{
cnt=0;
for(int i=1;i<=n;i++)
head[i]=-1;
for(int i=1;i<=n;i++)
{
for(int j=0;j q;
int u,v;
deep[s]=1;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
//cout<0&&!deep[v])
{
deep[v]=deep[u]+1;
q.push(v);
}
}
}
return deep[t]!=0;
}
//dfs增广路
int dfs(int u,int flow)
{
//cout<0&&deep[v]==deep[u]+1)
{
nowflow=dfs(v,min(flow,g[i].ca));
if(nowflow)
{
g[i].ca-=nowflow;
g[i^1].ca+=nowflow;
return nowflow;
}
}
}
return 0;
}
//Dinic算法
int Dinic(int s,int t)
{
int maxflow=0,flow;
while(bfs(s,t))
{
//当前弧优化
for(int i=1;i<=n;i++)
cur[i]=head[i];
//一次找多条增广路,Dinic核心
while(flow=dfs(s,inf))
{
maxflow+=flow;
}
}
return maxflow;
}
int main(void)
{
int t,u,v,w;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
Init();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
Dijs(1,n);
Build();
printf("%d\n",Dinic(1,n));
}
return 0;
}