HDU3416(KB11-O spfa+最大流)
Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4381 Accepted Submission(s): 1310
Problem Description
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.
So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?
Input
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.
Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.
At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.
Output
Sample Input
7 8
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
5 7 1
6 7 1
1 7
6 7
1 2 1
2 3 1
1 3 3
3 4 1
3 5 1
4 6 1
5 6 1
1 6
2 2
1 2 1
1 2 2
1 2
Sample Output
1
1
Author
Source
只有最短路上的边才加入网络,容量都为1,跑最大流
//2017-08-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[M]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} struct Dinic{
int level[N], S, T;
void init(){
tot = ;
memset(head, -, sizeof(head));
}
void set_s_t(int _S, int _T){
S = _S;
T = _T;
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
}
}dinic; //dis[0][u]表示从起点到u的最短距离,dis[1][u]表示从终点到u的最短距离,cnt[u]记录u入队次数,判负环用
int dis[][N], cnt[N];
bool vis[N];
bool spfa(int s, int n, int op){
memset(vis, false, sizeof(vis));
memset(dis[op], INF, sizeof(dis));
memset(cnt, , sizeof(cnt));
vis[s] = true;
dis[op][s] = ;
cnt[s] = ;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(dis[op][v] > dis[op][u] + w){
dis[op][v] = dis[op][u] + w;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} struct Line{
int u, v, w;
}line[M]; int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputO.txt", "r", stdin);
int T, n, m, A, B;
cin>>T;
while(T--){
cin>>n>>m;
int u, v, w;
for(int i = ; i < m; i++)
cin>>line[i].u>>line[i].v>>line[i].w;
cin>>A>>B;
dinic.init();
for(int i = ; i < m; i++)
add_edge(line[i].u, line[i].v, line[i].w);
spfa(A, n, );//求从起点开始到各个点的最短路
dinic.init();
for(int i = ; i < m; i++)
add_edge(line[i].v, line[i].u, line[i].w);
spfa(B, n, );//求从终点开始到各个点的最短路
dinic.init();
dinic.set_s_t(A, B);
for(int i = ; i < m; i++){
u = line[i].u;
v = line[i].v;
w = line[i].w;
//只有最短路上的边才加入网络
if(dis[][u]+dis[][v]+w == dis[][B]){
add_edge(u, v, );
add_edge(v, u, );
}
}
cout<<dinic.maxflow()<<endl;
}
return ;
}
HDU3416(KB11-O spfa+最大流)的更多相关文章
- bzoj 2285 [Sdoi2011]保密(二分,spfa + 最大流)
Description 现在,保密成为一个很重要也很困难的问题.如果没有做好,后果是严重的.比如,有个人没有自己去修电脑,又没有拆硬盘,后来的事大家都知道了. 当然,对保密最需求的当然是军方,其次才是 ...
- POJ 2175 spfa费用流消圈
题意:给出n栋房子位置和每栋房子里面的人数,m个避难所位置和每个避难所可容纳人数.然后给出一个方案,判断该方案是否最优,如果不是求出一个更优的方案. 思路:很容易想到用最小费用流求出最优时间,在与原方 ...
- 最长可重区间集 spfa费用流
给定实直线L上的n个开区间,和一个正整数k 选取若干个区间,在保证实直线L上的任意一个点最多被选出区间覆盖k次的情况下,使得这些区间的长度和最大 先把区间按照左端点排序, 考虑到重复其实就代表着相交, ...
- ZOJ 3362 Beer Problem(SPFA费用流应用)
Beer Problem Time Limit: 2 Seconds Memory Limit: 32768 KB Everyone knows that World Finals of A ...
- hdu3416+hdu6582(最短路+最大流)
题意 hdu3416: 给一个图,边不能重复选,问有多少个最短路 hdu6582: 给一个图,问最少删除边权多少的边后,最短路长度增加 分析 边不能重复选这个条件可以想到边权为1,跑最大流,所以我们可 ...
- Spfa费用流模板
; ,maxm=; ,fir[maxn],nxt[maxm],to[maxm]; int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int ...
- 洛谷.4015.运输问题(SPFA费用流)
题目链接 嗯..水题 洛谷这网络流二十四题的难度评价真神奇.. #include <queue> #include <cstdio> #include <cctype&g ...
- BZOJ.4819.[SDOI2017]新生舞会(01分数规划 费用流SPFA)
BZOJ 洛谷 裸01分数规划.二分之后就是裸最大费用最大流了. 写的朴素SPFA费用流,洛谷跑的非常快啊,为什么有人还T成那样.. 当然用二分也很慢,用什么什么迭代会很快. [Update] 19. ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
随机推荐
- cad.net之ACAD移植到GCAD的自动加载问题
将acad.pgp,lsp,fas,vlx,名称增加一份gcad.pgp,lsp,fas,vlx.涉及系统加载用. Lisp的拖拉加载在gcad无法通过lastprompt获取命令历史栏最后一行(含路 ...
- dubbo实现原理之SPI简介
dubbo采用微内核+插件体系,设计优雅,扩展性很强.微内核+插件体系是如何实现的呢?想必大家都知道SPI(service provider interface)机制.这种机制的原理是假如我们定义了服 ...
- Windows Service 项目中 Entity Framework 无法加载的问题
Windows Service 项目引用了别的类库项目,别的项目用到了 Entity Framework(通过Nuget引入),但是我的 Windows Service 无法开启,于是我修改了 App ...
- web安全之XSS注入
之前在做项目的时候有遇到一些安全问题,XSS注入就是其中之一 那么,什么是XSS注入呢? XSS又叫CSS (Cross Site Script) ,跨站脚本攻击.它指的是恶意攻击者往Web页面里插入 ...
- JVM锁优化
1. 概述 JDK1.6版本花费了大量精力去实现各种锁优化,如适应性自旋,锁消除,锁粗化,轻量级锁,偏向锁等,这些技术都是为了在线程期间更高效的共享数据,以及解决竞争问题. 2. 自旋锁与自适应自旋 ...
- yarn 学习 小记
官网:https://yarnpkg.com/zh-Hans/docs/installing-dependencies 简介:包管理工具,和npm类似主要特点:快速.安全.可靠 快速:本地安装包后,会 ...
- Java - 基础起步
package basics; //声明该类所在的包为basics,package为包的关键字 import java.util.Date; //导入java.util包中的Date类,用来封装当前的 ...
- [LeetCode]无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- 11-02 Java Object类使用详解
Object 作为超类 Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类. Object类的构造方法有一个,并且是无参构造,这其实就是理解当时我们说过,子类构造方法默认访 ...
- 线程中的定时器Timer类
Timer 定时器 几分钟之后执行一个任务. 创建了一个定时器相当于开启了一条线程,TimerTask相当于一个线程的任务.内部使用wait/notify机制来实现的. 用法非常的简单 就足以里面的 ...