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. CSU 1160 A(Contest #3)

    Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的 ...

  2. Canopy使用教程 (3)

    1. 2. plot函数: plot默认生成是曲线图,可以通过kind参数生成其他的图形,可选的值为:line, bar, barh, kde, density, scatter. 散点图.使用kin ...

  3. WPF 基础到企业应用系列索引

    转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...

  4. 华为V-ISA信誉安全体系:对付新型DDoS攻击的利器

        华为Anti-DDoS解决方案基于华为颇具传统优势的专业软硬件平台开发,在防护机制中,引入先进的检测机制,提供了业内首创的“V-ISA”信誉安全体系,是业界唯一单机可提供超百G DDoS防御能 ...

  5. mysql 命令行操作

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  6. IOS @2X.png

    [UIImage imageNamed:@"xxx.png"] 或者xib里iPhone4会自动找*@2x.png initWithContentOfFile:pathToImag ...

  7. BZOJ 2252 矩阵距离

    BFS. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  8. Maven Build profiles

    They modify the POM at build time, and are meant to be used in complementary sets to give equivalent ...

  9. MySQL 全文搜索支持

    MySQL 全文搜索支持 从MySQL 4.0以上 myisam引擎就支持了full text search 全文搜索,在一般的小网站或者blog上可以使用这个特性支持搜索. 那么怎么使用了,简单看看 ...

  10. Linux中的三个特殊文件

    stdin: 0 标准输入 stdout: 1 标准输出 stderr : 2 标准错误输出 /dev/null 表示一个只写文件.所有写入到这个文件的信息都会丢失