HDU5889 spfa保存最短路径,dicnic + 当前弧优化 求最小割

                                            Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2418    Accepted Submission(s): 696


 

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(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

 

 

Source

2016 ACM/ICPC Asia Regional Qingdao Online

 

 


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
const int maxm = 10000 + 10;
typedef pair<int,int> P;
int n,m;
int l[maxn];//记录层数
int h[maxn];//链式前向星
int cur[maxn];
int tot = 0;
struct edge
{
  int to;
  int c;
  int next;
  edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
}es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
    es[tot] = edge(v,c,h[u]);
    h[u] = tot++;
}



bool bfs(int s, int t)
{
   memset(l,0,sizeof(l));
   l[s] = 1;
   queue <int> q;
   q.push(s);
   while(!q.empty())
   {
    int u = q.front();
    q.pop();
    if(u == t)  return true;
    for(int i = h[u]; i != -1; i = es[i].next)
        {
         int v = es[i].to;
         if(!l[v] && es[i].c) {l[v] = l[u] + 1; q.push(v);}
        }
   }
   return false;
}

int dfs(int x, int t, int mf)
{
    if(x == t) return mf;
    int ret = 0;
    for(int &i = cur[x]; i != -1; i = es[i].next)
    {
      if(es[i].c && l[x] == l[es[i].to] - 1)
      {
        int f = dfs(es[i].to,t,min(es[i].c,mf - ret));
        es[i].c -= f;
        es[i^1].c += f;
        ret += f;
        if(ret == mf) return ret;
      }
    }
    return ret;
}

int dinic(int s, int t)
{
  int ans = 0;
  while(bfs(s,t))  {
    for(int i = 0; i <= n; i++) cur[i] = h[i];//注意要把所有的赋值
    ans += dfs(s,t,INF);
  }
  return ans;
}

vector<int> g[maxn];
map<P,int> mp;
bool inq[maxn];
int d[maxn];

void spfa()
{
   memset(d,INF,sizeof(d));
   memset(inq,false,sizeof(inq));
   queue<int> Q;
   Q.push(1);
   d[1] = 0;
   inq[1] = true;
   while(!Q.empty())
   {
     int u = Q.front();
     Q.pop();
     inq[u] = false;
     if(d[u] > d[n]) return ;
     for(int i = 0; i < g[u].size(); i++)
       {
         int v = g[u][i];
         if(d[v] > d[u] + 1)
         {
           d[v] = d[u]+1;
           if(!inq[v]) {inq[v] = true;Q.push(v);}
        }
     }
   }
}


int main()
{
  int T;
  scanf("%d",&T);
  while(T--)
  {
   scanf("%d%d",&n,&m);
   tot = 0;
   for(int i = 0; i <= n; i++)  g[i].clear();
   mp.clear();
   memset(h,-1,sizeof(h));
   int u,v,c;
   for(int i = 0; i < m; i++)
   {
    scanf("%d%d%d",&u,&v,&c);
    g[u].push_back(v);
    g[v].push_back(u);
    mp[P(max(u,v),min(u,v))] = c;
   }
   spfa();
   for(int i = 1; i <= n; i++)
       for(int j = 0; j < g[i].size(); j++)
            if(d[g[i][j]]-d[i] == 1)
          {
             add_edge(i,g[i][j],mp[P(max(g[i][j],i),min(g[i][j],i))]);
             add_edge(g[i][j],i,0);
             //cout << i << " " << g[i][j] << endl;
           }
   int ans = dinic(1,n);
   printf("%d\n",ans);
   }
   return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值