Barricade


Problem Description
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(N1000)  and  M(M10000) .
The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  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
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <ctime> #include <cstdio> #include <climits> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread(ch) freopen(ch,"r",stdin) #define fwrite(ch) freopen(ch,"w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const double eps = 1e-8; const int maxn = 1111,maxm = 11111; struct Edge {     int v,w,next; } eg[maxm*5],teg[maxm*5]; int ds[maxn]; bool vis[maxn]; int head[maxn],thead[maxn]; int n,tp,ttp,st,en; void Add(int u,int v,int w) {     eg[tp].v = v;//指向节点的节点;     eg[tp].w = w;//权值;     eg[tp].next = head[u];//相当于逆序建表;     head[u] = tp++;     eg[tp].v = u;     eg[tp].w = 0;     eg[tp].next = head[v];     head[v] = tp++; } void add(int u,int v,int w) {     teg[ttp].v = v;     teg[ttp].w = w;     teg[ttp].next = thead[u];     thead[u] = ttp++; } int dis[maxn]; int gap[maxn]; int pre[maxn]; int cur[maxn]; int isap() {     memset(dis,0,sizeof(dis));     memset(gap,0,sizeof(gap));     memset(pre,-1,sizeof(pre));     memset(cur,-1,sizeof(cur));     gap[0] = n;     int u,v,flow,ans;     flow = INF;     u = pre[0] = 0;     ans = 0;     while(dis[1] < en)     {         for(int &i = cur[u]; i != -1; i = eg[i].next)         {             if(eg[i].w && dis[eg[i].v]+1 == dis[u]) break;         }         if(cur[u] != -1)         {             v = eg[cur[u]].v;             flow = min(flow,eg[cur[u]].w);             pre[v] = u;             u = v;             if(u == en)             {                 for(; u != 0; u = pre[u])                 {                     eg[cur[pre[u]]].w -= flow;                     eg[cur[pre[u]]^1].w += flow;                 }                 ans += flow;                 flow = INF;             }         }         else         {             int mn = n;             for(int i = head[u]; i != -1; i = eg[i].next)             {                 if(eg[i].w && dis[eg[i].v] < mn)                 {                     mn = dis[eg[i].v];                     cur[u] = i;                 }             }             if(!(--gap[dis[u]])) break;             dis[u] = mn+1;             gap[dis[u]]++;             u = pre[u];         }     }     return ans; } void bfs() {     queue <int> q;     memset(vis,0,sizeof(vis));     memset(ds,INF,sizeof(ds));     ds[0] = 0;     q.push(0);     int u,v;     while(!q.empty())     {         u = q.front();         q.pop();         vis[u] = 0;         for(int i = thead[u]; i != -1; i = teg[i].next)         {             v = teg[i].v;             if(ds[v] > ds[u]+1)             {                 ds[v] = ds[u]+1;                 if(!vis[v])                 {                     q.push(v);                     vis[v] = 1;                 }             }         }     } } int main() {     //fread();     //fwrite();     int t;     int m;     int u,v,w;     scanf("%d",&t);     while(t--)     {         scanf("%d%d",&n,&m);         memset(head,-1,sizeof(head));         memset(thead,-1,sizeof(thead));         tp = ttp = 0;         st = 0,en = n-1;         for(int i = 0; i < m; ++i)         {             scanf("%d%d%d",&u,&v,&w);             u--,v--;             add(u,v,w);             add(v,u,w);         }         bfs();         for(int i = 0; i < n; ++i)         {             for(int j = thead[i]; j != -1; j = teg[j].next)             {                 u = i;                 v = teg[j].v;                 if(ds[v] == ds[u]+1)                 {                     //printf("%d %d\n",u,v);                     Add(u,v,teg[j].w);                 }             }         }         printf("%d\n",isap());     }     return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值