http://acm.hdu.edu.cn/showproblem.php?pid=5889

题意:

给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点。现在帝国将军要在路径上放置障碍,每条边上都有一个放置障碍的代价。求至少需要多少代价。

思路:

首先就是求最短路,然后将最短路上的边重新进行构图跑最小割即可。

一开始求了两遍bfs,分别求出起点到各个点的距离和终点到各个点的距离,然后去判断每条边是否在最短路中,但是这样的话在最大流的构图中无法确定方向,然后就一直Wa。。。

其实跑一遍就够了。。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n,m; vector<pll> G[maxn];
bool vis[maxn];
int d[maxn]; void bfs(int s)
{
memset(vis,false,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=;
vis[s]=true;
while(!Q.empty())
{
int u=Q.front(); Q.pop();
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
if(!vis[v])
{
vis[v]=true;
d[v]=d[u]+;
Q.push(v);
}
}
}
} struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
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,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(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]=true;
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[e.to]==d[x]+ && (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()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
}
bfs();
DC.init(n+);
for(int i = ;i<=n;i++)
for(int j = ;j<G[i].size();j++)
if(d[G[i][j].first]==d[i]+)
DC.AddEdge(i,G[i][j].first,G[i][j].second);
printf("%d\n",DC.Maxflow(,n));
}
return ;
}

HDU 5889 Barricade(最短路+最小割)的更多相关文章

  1. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

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

  2. Barricade HDU - 5889(最短路+最小割)

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

  3. hdu 6852Path6(最短路+最小割)

    传送门 •题意 有n个城市,标号1-n 现花费最小的代价堵路 使得从1号城市到n号城市的路径边长 (注意只是变长不是最长) 堵一条路的代价是这条路的权值 •思路 在堵路以前,从1到n的最小路径当然是最 ...

  4. HDU - 6582 Path (最短路+最小割)

    题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...

  5. 【bzoj1266】[AHOI2006]上学路线route 最短路+最小割

    题目描述 可可和卡卡家住合肥市的东郊,每天上学他们都要转车多次才能到达市区西端的学校.直到有一天他们两人参加了学校的信息学奥林匹克竞赛小组才发现每天上学的乘车路线不一定是最优的. 可可:“很可能我们在 ...

  6. HDU 5889 Barricade(最短路+最小割水题)

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

  7. [2019杭电多校第一场][hdu6582]Path(最短路&&最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 题意:删掉边使得1到n的最短路改变,删掉边的代价为该边的边权.求最小代价. 比赛时一片浆糊,赛后 ...

  8. HDU 4289:Control(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...

  9. HDU 3452 Bonsai(网络流之最小割)

    题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...

随机推荐

  1. [MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程

    [MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程 http://mysqllover.com/?p=581 尽管Mariadb以及Facebook在long ...

  2. golang使用vendor目录来管理依赖包

    Vendor目录介绍 随着Go 1.5 release版本的发布,vendor目录被添加到除了GOPATH和GOROOT之外的依赖目录查找的解决方案.在Go 1.6之前,你需要手动的设置环境变量GO1 ...

  3. 使用LinkedList模拟栈数据结构的集合

    封装MyStack类 public class MyStack { private LinkedList link; //调用MyStack创建对象的时候其实是调用的LinkedList创建的是Lin ...

  4. SDUT1157:小鼠迷宫问题(bfs+dfs)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1157 题目描述 小鼠a与小鼠b身处一个m×n的 ...

  5. [ps] 灰度和通道基础知识

    灰度.灰度值.灰度图像 灰度:灰度使用黑色调来表示物体,即用黑色为基准色,不同饱和度的黑色来显示图像.每个灰度对象都具有从0%(白色)到100%(黑色)的亮度值.使用黑白或灰度扫描仪生成的图像通常以灰 ...

  6. Quick Look at the Air Jordan 32

    A color with 25 years of history in the Air Jordan line will once again leave its mark on the Air Jo ...

  7. window.open和window.showModalDialog

    window.open window.open是打开一个新窗口 在window.open打开的窗口中刷新父页面 opener.location.reload(); 打开一个窗口格式:属性可以任意设置 ...

  8. Leetcode: Binary Tree Level Order Transversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. 【Redis学习之三】Redis单节点安装

    本文介绍两个版本:redis-2.8.18.tar.gz 和 redis-3.0.0-rc2.tar.gz,均采用源码安装方式 一.redis3.0 部署环境 redis-3.0.0-rc2.tar. ...

  10. NIO_2

    导语 缓冲器的设计的是新IO模型中最基础的一部分.因为新IO模型中要求所有的IO操作都需要进行缓冲.在新的IO模型中,不再向输出流写入数据和从数据流中读取数据了,而是要从缓冲区中读写数据.缓冲区可是是 ...