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跑一遍最短路, ...
随机推荐
- NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解
肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...
- Linux试题
1.编写脚本,统计/etc./usr./var目录中有多少个一级子目录和文件 #!/bin/bash # danran # time is Mon Jun 5 13:09:12 CST 2017 li ...
- AIM Tech Round (Div. 2) A
A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- simpleDateFormat的 学习
http://blog.csdn.net/qq_27093465/article/details/53034427
- [ubuntu]对指定区域截图
ctrl+shift 鼠标变成正十字. 按住右键就可以随意截图了. 设置方法: 打开系统设置面板 system settings --> keyboard --> shortcuts - ...
- DOM基本代码一
dom学习基本代码第一部分 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- git--------------bug修复流程
当前所有分支: master:主分支 test:测试分支 zs:开发人员分支 ls:开发人员分支 场景:zs正在开发A模块功能,线上环境产生了一个bug. zs的操作流程(当前分支为zs分支): 1. ...
- springboot-用logback将日志文件按等级保存到不同文件
springboot-用logback将日志文件按等级保存到不同文件 案例: 例如项目基本包名为com.xxx,将该包下的所有日志按debug.info.warn.error等级分别保存到D:/log ...
- Tomcat免安装版+Eclipse配置
Tomcat是目前比较流行的开源且免费的Web应用服务器,在我的电脑上第一次安装Tomcat,再经过网上教程和自己的摸索后,将这个过程 重新记录下来,以便以后如果忘记了可以随时查看. 注意:首先要明确 ...
- 51Nod 1004 n^n末尾数字 | 快速幂
#include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...