Barricade

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

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
【分析】给你一个无向图,现有敌人要从n点走到1点且他只走最短路(每条路长度一样)。为了阻止敌人到达1点,要求在某些路上设置障碍,使得敌人最少能遇到一个障碍。
 其实就是在最短路上跑网络流,因为最小割就是最大流。一开始一直超时,后来问的学长才知道我的Dinic板子有问题,没有当前弧优化,然后改了一下,就过了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
struct Node
{
int v,w;
Node(int vv,int ww):v(vv),w(ww){};
};
vector<Node>e[N];
int s,t,n,m,vs,vt;
int d[N];
int vis[N];
void spfa()
{
memset(vis,,sizeof(vis));
for(int i = ;i<=n;i++)
d[i]=inf;
d[s]=;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u]=;
for(int i = ;i<e[u].size();i++)
{
int v = e[u][i].v;
if(d[v]>d[u]+)
{
d[v]=d[u]+;
if(!vis[v])
q.push(v);
vis[v]=;
}
}
}
} struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic
{
int s,t;
vector<Edge>edges;
vector<int> G[N];
bool vis[N];
int d[N];
int cur[N];
void init()
{
for (int i=;i<=n+;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
int mm=edges.size();
G[from].push_back(mm-);
G[to].push_back(mm-);
}
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while (!q.empty())
{
int x = q.front();q.pop();
for (int i = ;i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow)
{
vis[e.to]=;
d[e.to] = d[x]+;
q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if (x==t || a==)
return a;
int flow = ,f;
for(int &i=cur[x];i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if (d[x]+ == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if (a==)
break;
}
}
return flow;
} int Maxflow(int s,int t)
{
this->s=s;
this->t=t;
int flow = ;
while (BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(s,inf);
}
return flow;
}
}dc; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i = ;i<=n;i++)
e[i].clear();
for(int i = ;i<=m;i++)
{
int u,v,di;
scanf("%d%d%d",&u,&v,&di);
e[u].push_back(Node(v,di));
e[v].push_back(Node(u,di));
}
s=,t=n;
spfa();
dc.init();
for(int i = ;i<=n;i++)
for(int j = ;j<e[i].size();j++)
if(d[e[i][j].v]==d[i]+)
dc.AddEdge(i,e[i][j].v,e[i][j].w);
printf("%d\n",dc.Maxflow(s,t));
}
}

HDU5889 Barricade(最短路)(网络流)的更多相关文章

  1. HDU 5889 (最短路+网络流)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. SGU 185 Two shortest ★(最短路+网络流)

    [题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...

  3. hdu3416 Marriage Match IV(最短路+网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意: 给出含n个点.m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有 ...

  4. bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...

  5. Barricade---hdu5889(最短路+网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意:有n个点m条边,每条边的长度相同,我们可以默认为1,构成一个无向图:现在起点为1,终点为n ...

  6. hdu-3416(最短路+网络流)

    题意:给你一个有向权图,问你从S到E有几条最短路,每条边直走一次的情况下: 解题思路:每条边直走一次,最大流边权为1,因为要算几条最短路,那么能得到最短路的路径标记下,然后跑最大流 代码: #incl ...

  7. hdu-5889-最短路+网络流/最小割

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. [bzoj3931][CQOI2015]网络吞吐量——最短路+网络流

    题目 传送门 题解 第一次一遍就AC一道bzoj上的题,虽然是一道水题... 我们做一边最短路,求出每个点的dist,然后再做一次类似spfa的操作,求出每个点是否可以用于建图. 在新图上拆点跑一边d ...

  9. hdu多校第一场1005(hdu6582)Path 最短路/网络流

    题意: 在无向图上删边,让此图上从起点到终点的最短路长度变大,删边的代价是边长,求最小代价. 题解: 先跑一遍迪杰斯特拉,求出所有点的d[]值,然后在原图上保留所有的边(i,j)仅当i,j满足d[j] ...

随机推荐

  1. URAL 1519 基础插头DP

    题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...

  2. hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏

    IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...

  3. Android TextView多行垂直滚动

    在Android应用中,有时候需要TextView可以垂直滚动,今天我就介绍一下怎么实现的.在布局里: <TextView android:id="@+id/tvCWJ" a ...

  4. checkbox的全选、反选、删除(MainActivity)

    package com.example.ay; import java.util.ArrayList;import java.util.List; import com.example.adapter ...

  5. JVM-运行时数据区

                                                                              运行时数据区示意图                 ...

  6. Windows下LDAP服务器配置

    LDAP即轻量级目录访问协议(Lightweight Directory Access Protocol),基础知识不再赘述,本文主要记录我的配置与安装过程. LDAP for windows下载 o ...

  7. JavaScript 之 走马灯

    1.原理分析:首先截取字符串的最后一位用Last表示,再截取剩余字符串用Rest表示,拼接字符串Last + Rest, 此事字符串是不会动的,还需要一个函数setInterval(javascrip ...

  8. PAT 07-2 A+B和C

    有两个值得注意的地方:1.变长数组(VLA)的使用,没想到PAT上的OJ竟然支持C99,一开始不知道就没用,看了看别人的,既然,还是用吧, 它有一点我不太喜欢,它不能像一般数组那样在声明时通过赋一个0 ...

  9. 浅谈Ddos攻击攻击与防御

    EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-2-10From: http://www.80sec.com/ [ 目录 ]一 ...

  10. “java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp”

    最近在项目中使用hibernate查询时,总报错“java.sql.SQLException: Value '0000-00-00' can not be represented as java.sq ...