HDU5889 Barricade(最短路)(网络流)
Barricade
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 604 Accepted Submission(s): 172
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.
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.
4 4
1 2 1
2 4 2
3 1 3
4 3 4
#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(最短路)(网络流)的更多相关文章
- HDU 5889 (最短路+网络流)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- SGU 185 Two shortest ★(最短路+网络流)
[题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...
- hdu3416 Marriage Match IV(最短路+网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意: 给出含n个点.m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有 ...
- bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流
3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...
- Barricade---hdu5889(最短路+网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意:有n个点m条边,每条边的长度相同,我们可以默认为1,构成一个无向图:现在起点为1,终点为n ...
- hdu-3416(最短路+网络流)
题意:给你一个有向权图,问你从S到E有几条最短路,每条边直走一次的情况下: 解题思路:每条边直走一次,最大流边权为1,因为要算几条最短路,那么能得到最短路的路径标记下,然后跑最大流 代码: #incl ...
- hdu-5889-最短路+网络流/最小割
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- [bzoj3931][CQOI2015]网络吞吐量——最短路+网络流
题目 传送门 题解 第一次一遍就AC一道bzoj上的题,虽然是一道水题... 我们做一边最短路,求出每个点的dist,然后再做一次类似spfa的操作,求出每个点是否可以用于建图. 在新图上拆点跑一边d ...
- hdu多校第一场1005(hdu6582)Path 最短路/网络流
题意: 在无向图上删边,让此图上从起点到终点的最短路长度变大,删边的代价是边长,求最小代价. 题解: 先跑一遍迪杰斯特拉,求出所有点的d[]值,然后在原图上保留所有的边(i,j)仅当i,j满足d[j] ...
随机推荐
- jquery easyui DataGrid 动态的改变列显示的顺序
$.extend($.fn.datagrid.methods,{ columnMoving: function(jq){ return jq.each(function(){ var target = ...
- Cisco IOS Debug Command Reference I through L
debug iapp through debug ip ftp debug iapp : to begin debugging of IAPP operations(in privileged EXE ...
- 计算文字的高度和宽度--以微博会话界面中用户名(userName)为例
所用方法 // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing t ...
- iOS开发之通知使用总结
通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...
- 新发布GoldenGate 12c版本中的主要特性
业界领先的实时数据集成工具GoldenGate现在可以帮助企业在传统数据库和云平台.大数据平台之间进行实时复制.新的OGG 12c支持更多的异构数据库和大数据平台,进一步提升可管理性和对混合云 ...
- PHP+MySql字符问题原理分析
假如数据库已经设置了utf-8 ,php文件也设置了utf-8 ,但在php文件的查询语句中未添加了 mysql_query("set names utf8")语句,此时php页面 ...
- 2016-1-3点菜系统demo的实现,pickerView的学习
// // ViewController.m // pickView // // Created by Mac on 16/1/3. // Copyright © 2016年 Mac. All rig ...
- copy和assign的使用和区别
1.使用copy和assign都可以进行修饰属性或者变量. 2.区别: (1)copy的使用:使用这个进行修饰的属性,当已经进行初始化之后,就无法再改变属性的数据. 如: @property (cop ...
- 在VS2010 中兼容Qt4和Qt5
1,同时安装Qt4和Qt5 Qt_add,然后在 2. 如果之前的项目使用Qt4编写的,如果新添加新的类和ui的话,一定要选择Qt Add_in 1.1.11,不然就无法生成moc文件,随便选择 Ch ...
- Linux下控制器IO地址
在Linux下使用cat /proc/ioports可以查看控制器使用的IO地址范围