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. K2任命新的亚太区高级副总裁

    K2, 一个屡获殊荣的企业应用软件公司宣布,任命陈光明(Tan Kwang Meng, KM)为亚太区高级副总裁.这次任命是对公司持续发展的肯定,同时也是对将亚太区作为全球扩张战略的关键市场的承诺. ...

  2. android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理

    本示例说明: 1.自定义listview条目样式,自定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,并且操作后listview自动刷新. 3.响应用户操作点击事 ...

  3. PL/SQL : Procedural Language / Structual Query Language and it is an exrension to SQL.

    SQL is not very flexible and it cannot be made to react differently to differing sutuations easily. ...

  4. ByteArray

    ByteArray:属性endian:String == Endian.BIG_ENDIAN/Endian.LITTLE_ENDIAN.length:uint ByteArray的字节数positio ...

  5. 多功能节点连线绘图控件Nevron Diagram for .NET使用方法及下载地址

    Nevron Diagram for .NET是一个功能强大,世界上顶级的.NET图表控件.可扩展的图形报表构架,可以帮您创建功能丰富的Winforms及Webforms图表解决方案.这个产品构建于N ...

  6. 数组的filter方法

    filter()函数用于过滤序列,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素. eg: var arr=[10,11,12,13,14 ...

  7. UITableViewCell 自适应高度 ios8特性

    这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度.自动计算 cell 高度的功能使用 iOS ...

  8. SVG 2D入门7 - 重用与引用

    前面介绍了很多的图形元素,如果很多图形本身是一样的,需要每次都去定义一个新的么?我们能否共用一些图形呢?这是这节的重点 - SVG元素的重用. 组合 - g元素      g元素是一种容器,它组合一组 ...

  9. HID高级攻击姿势:利用PowerShell脚本进行文件窃取

    0×01 引言 又到了期中考试了,我又要去偷答案了,一直发现远程下载运行exe的方式不太好,容易报毒所以这里打算用ps脚本. 0×02 关于HID HID是Human Interface Device ...

  10. js中的apply调用

    今天看了阮一锋老师的一篇文章,感觉很明了对闭包的理解,尤其是文章中的apply的介绍 apply()是函数对象的一个方法,它的作用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象. ...