HDU 5889 (最短路+网络流)
Barricade
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1117 Accepted Submission(s): 340
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 <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
int n,m;
int g[maxn][maxn];
int vis[maxn];
int dis[maxn];
struct edge
{
int to;
int cap;
int rev;
};
vector<edge> gg[maxn];
int level[maxn];
int it[maxn];
void add(int from,int to,int cap)
{
edge cur;
cur.to = to;
cur.cap = cap;
cur.rev = gg[to].size();
gg[from].push_back(cur);
cur.to = from;
cur.cap = ;
cur.rev = gg[from].size()-;
gg[to].push_back(cur);
} void bfs(int s)
{
memset(level,-,sizeof(level));
queue<int> q;
level[s] = ;
q.push(s);
while(!q.empty())
{
int v = q.front(); q.pop();
for(int i=;i<gg[v].size();i++)
{
edge &e = gg[v][i];
if(e.cap>&&level[e.to]<)
{
level[e.to] = level[v]+;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t) return f;
for(int &i=it[v];i<gg[v].size();i++)
{
edge &e = gg[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d = dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap -= d;
gg[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
}
int max_flow(int s,int t)
{
int flow = ;
for(;;)
{
bfs(s);
if(level[t]<) return flow;
memset(it,,sizeof(it));
int f;
while((f=dfs(s,t,inf))>) flow += f;
}
}
bool bfs1()
{
queue<int> q;
memset(vis,,sizeof(vis));
memset(dis,inf,sizeof(dis));
vis[] = ;
dis[] = ;
q.push();
while(!q.empty())
{
int cur = q.front();q.pop();
if(cur==n) return true;
for(int i=;i<=n;i++)
{
if(cur==i) continue;
if(!vis[i]&&g[cur][i]!=-)
{
vis[i] = ;
dis[i] = dis[cur]+;
q.push(i);
}
}
}
return false;
}
void bfs2()
{
queue<int> q;
memset(vis,,sizeof(vis));
vis[n] = ;
q.push(n);
while(!q.empty())
{
int cur = q.front();q.pop();
for(int i=;i<=n;i++)
{
if(cur==i) continue;
if(g[cur][i]==-) continue;
if(dis[i]+==dis[cur])
{
add(i,cur,g[i][cur]);
if(!vis[i])
{
vis[i] = ;
q.push(i);
}
}
}
}
}
int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d %d",&n,&m);
int u,v,w;
memset(g,-,sizeof(g));
for(int i=;i<maxn;i++) gg[i].clear();
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
g[u][v] = w;
g[v][u] = w;
}
int ans = ;
bfs1();
bfs2();
ans = max_flow(,n);
printf("%d\n",ans);
}
return ;
}
HDU 5889 (最短路+网络流)的更多相关文章
- HDU 5889 Barricade(最短路+最小割水题)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU 4289 Control (网络流,最大流)
HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...
- HDU 4292 Food (网络流,最大流)
HDU 4292 Food (网络流,最大流) Description You, a part-time dining service worker in your college's dining ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- 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 ...
随机推荐
- oracle导入导出数据库
oracle导出dmp文件: 开始->运行->输入cmd->输入 exp user/password@IP地址:1521/数据库实例 file=文件所在目录 (如:exp user/ ...
- 基于ZooKeeper的分布式Session实现
1. 认识ZooKeeper ZooKeeper—— “动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而不是像走在原始丛林里,心惊胆颤的被 ...
- C#生成word
using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Co ...
- 移动UI
UI设计需要关注:色彩,信息布局,交互流程,用户体验,尺寸等
- MFC中MessageBeep与sndPlaySound播放声音函数使用
MessageBeep(0x00000000L); //用来播放系统默认音频文件,如0x00000000L为系统提示音,具体音频对应规则,请参照MSDN. sndPlaySound函数用来播放指 ...
- html/css技巧总结
.e-select .on{display:none} on为e-select的子元素,(之间有空格).e-select.on{display:block} 只有两种属性同时存在时才会起作用(之间无空 ...
- hdu_5800_To My Girlfriend(变种背包)
题目链接:hdu_5800_To My Girlfriend 题意: 给你n和物品和一个重量m,让你求 题解: To My Girlfriend 令dp[i][j][s1][s2]表示前i个物品填了j ...
- hdu_5794_A Simple Chess(lucas+dp)
题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...
- [转]phpcms 判断用户登录代码
<!-- 用户登录开始 --> <?php //读取用户信息 $ypzy_userid = param::get_cookie('_userid'); if(!empty($ypzy ...
- listview前几个item的图片怎么是空白的、listview更新了ui不起作用、在handler里更新了UI不起作用
不是不起作用,不信你可以在更新ui代码附近加输出的log,说明程序是跑到那里了.但是未达到我们的想要的效果. 我们知道在listview里更新UI,listview的适配器Adapter里有个getV ...