hdu 3416(最大流+最短路)
Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3368 Accepted Submission(s): 1001
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?
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.
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
1
1
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int v,w,next;
}edge[M];
int head[N];
int level[N];
int tot;
void init()
{
memset(head,-,sizeof(head));
tot=;
}
void addEdge(int u,int v,int w,int &k)
{
edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
edge[k].v = u,edge[k].w=,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
queue<int>q;
memset(level,,sizeof(level));
level[src]=;
q.push(src);
while(!q.empty())
{
int u = q.front();
q.pop();
if(u==des) return ;
for(int k = head[u]; k!=-; k=edge[k].next)
{
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==&&w!=)
{
level[v]=level[u]+;
q.push(v);
}
}
}
return -;
}
int dfs(int u,int des,int increaseRoad){
if(u==des||increaseRoad==) {
return increaseRoad;
}
int ret=;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(level[v]==level[u]+&&w!=){
int MIN = min(increaseRoad-ret,w);
w = dfs(v,des,MIN);
if(w > )
{
edge[k].w -=w;
edge[k^].w+=w;
ret+=w;
if(ret==increaseRoad){
return ret;
}
}
else level[v] = -;
if(increaseRoad==) break;
}
}
if(ret==) level[u]=-;
return ret;
}
int Dinic(int src,int des)
{
int ans = ;
while(BFS(src,des)!=-) ans+=dfs(src,des,INF);
return ans;
}
struct Edge1{
int v,w,next;
}edge1[M],edge2[M];
int head1[N],head2[N];
int tot1,tot2;
int n,m,a,b;
void addEdge1(int u,int v,int w,int &k){
edge1[k].v = v,edge1[k].w=w,edge1[k].next = head1[u],head1[u]=k++;
}
void addEdge2(int u,int v,int w,int &k){
edge2[k].v = v,edge2[k].w=w,edge2[k].next = head2[u],head2[u]=k++;
}
void init1(){
memset(head1,-,sizeof(head1));
tot1 = ;
}
void init2(){
memset(head2,-,sizeof(head2));
tot2 = ;
}
int low[][N];
bool vis[N];
void spfa(int s,int t,int flag,int *head,Edge1 edge[]){
for(int i=;i<=n;i++){
low[flag][i] = INF;
vis[i] = false;
}
low[flag][s] = ;
queue<int >q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(low[flag][v]>low[flag][u]+w){
low[flag][v] = low[flag][u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
init1();
init2();
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
addEdge1(u,v,w,tot1);
addEdge2(v,u,w,tot2); ///反向边
}
scanf("%d%d",&a,&b);
spfa(a,b,,head1,edge1); ///a->b 第一遍
spfa(b,a,,head2,edge2); ///b->a 第二遍
init();
for(int i=;i<=n;i++){
for(int j=head1[i];j!=-;j=edge1[j].next){
int v = edge1[j].v,w=edge1[j].w;
if(low[][i]+low[][v]+w==low[][b]){
addEdge(i,v,,tot);
}
}
}
if(low[][b]==INF){
printf("0\n");
continue;
}
int max_flow = Dinic(a,b);
printf("%d\n",max_flow);
}
return ;
}
hdu 3416(最大流+最短路)的更多相关文章
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- HDU - 3416-Marriage Match IV (最大流 + 最短路)
HDU - 3416:http://acm.hdu.edu.cn/showproblem.php?pid=3416 参考:https://www.cnblogs.com/kuangbin/archiv ...
- HDU 1532 最大流入门
1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...
- java8学习之流的短路与并发流
并发流: 从api的角度来看,其实跟咱们之前一直在用的stream()方式差不多,但是底层是有明显的不同,所以这里初步先对并发流有一个基本的认识, 说到串行与并行,最直观的感受就是效率的不同,所以下面 ...
- HDU 3416:Marriage Match IV(最短路+最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意:给出n个点m条边,边信息分别是两个端点和一个费用,再给出一个起点和一个终点,问从起点到终点的完全不相 ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- O - Marriage Match IV - hdu 3416(最短路+最大流)
题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次. 分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么 ...
- HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】
<题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...
随机推荐
- 如何区别java中的public,protected,default,private
================Public====================== 1>首先我们介绍public关键字,从字面意义上出发,public意为公共的,可见它的访问权限是很宽松的 ...
- linux 使用vim文件加密/解密的方法
一. 利用 vim/vi 加密:优点:加密后,如果不知道密码,就看不到明文,包括root用户也看不了:缺点:很明显让别人知道加密了,容易让别人把加密的文件破坏掉,包括内容破坏和删除: vi编辑器相信大 ...
- mysql的时间函数整理
转:这里总结的非常齐全: http://fengbin2005.iteye.com/blog/1999763 Mysql时间函数 对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描 ...
- HDU3579 线性同余方程(模板 余数不一定互质)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- Qt ------ 初始化构造函数参数,parent
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setup ...
- 名人问题/名流问题/Celebrity
问题描述:名人问题一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人.现在有一个N个人的集合,以及他们之间的认识关系.求一个算法找出其中的名人(如果有的话)或者判断出没有名人(如果没 ...
- C++指针与数组
对数组地址的理解,如 int c[2] = {2,3}; int(*cp)[2] = &c; cout << &c[0] << c << cp &l ...
- Test Index
top1 top11 top2 top1 top11 top2
- HDU 5950Recursive sequence ICPC沈阳站
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- [Luogu 1533] 可怜的狗狗
平衡树,我用的SBT. 排一下序尽量减少操作次数. 第K大询问. 以及插入删除. #include <algorithm> #include <cstdio> #include ...