HDU3416 Marriage Match IV —— 最短路径 + 最大流
题目链接:https://vjudge.net/problem/HDU-3416
Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4710 Accepted Submission(s): 1412
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
题意:
求有多少条最短路径,要求边不能够重叠(但题目可以有重边,即两个点之间可以有几条边)。
题解:
1.先用spfa算法求出最短路径,并且筛掉那些不在最短路径上的边。
2.构建网络流图:对于在最短路径上的边u-->v,在网络流图中构建一条边 u-->v,且边权为1,表明这条边只能被走一次。
3.以起点为源点,以终点为汇点,求出最大流,即为答案。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; vector<pair<int,int> >son[MAXN];
int dis1[MAXN], dis2[MAXN], dis[MAXN], inq[MAXN];
void spfa(int start, int N)
{
memset(inq, , sizeof(inq));
for(int i = ; i<=N; i++)
dis[i] = INF; queue<int>Q;
Q.push(start);
inq[start] = ;
dis[start] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = ; i<son[u].size(); i++)
{
int v = son[u][i].first;
int w = son[u][i].second;
if(dis[v]>dis[u]+w)
{
dis[v] = dis[u]+w;
if(!inq[v])
{
Q.push(v);
inq[v] = ;
}
}
}
}
} struct Edge
{
int to, next, cap, flow;
}edge[MAXM<<];
int tot, head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN]; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
} int sap(int start, int end, int nodenum)
{
memset(dep, , sizeof(dep));
memset(gap, , sizeof(gap));
memcpy(cur, head, sizeof(head));
int u = pre[start] = start, maxflow = ,aug = INF;
gap[] = nodenum;
while(dep[start]<nodenum)
{
loop:
for(int i = cur[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap-edge[i].flow && dep[u]==dep[v]+)
{
aug = min(aug, edge[i].cap-edge[i].flow);
pre[v] = u;
cur[u] = i;
u = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u,u = pre[u])
{
edge[cur[u]].flow += aug;
edge[cur[u]^].flow -= aug;
}
aug = INF;
}
goto loop;
}
}
int mindis = nodenum;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap-edge[i].flow && mindis>dep[v])
{
cur[u] = i;
mindis = dep[v];
}
}
if((--gap[dep[u]])==)break;
gap[dep[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} int u[MAXM], v[MAXM], w[MAXM];
int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
int start, end;
scanf("%d%d", &n,&m);
for(int i = ; i<=m; i++)
scanf("%d%d%d", &u[i], &v[i], &w[i]);
scanf("%d%d", &start, &end); for(int i = ; i<=n; i++) son[i].clear();
for(int i = ; i<=m; i++) son[u[i]].push_back(make_pair(v[i], w[i]));
spfa(start, n);
memcpy(dis1, dis, sizeof(dis1));
int distance = dis1[end]; for(int i = ; i<=n; i++) son[i].clear();
for(int i = ; i<=m; i++) son[v[i]].push_back(make_pair(u[i], w[i]));
spfa(end, n);
memcpy(dis2, dis, sizeof(dis2)); init();
for(int i = ; i<=m; i++)
if(dis1[u[i]]+w[i]+dis2[v[i]]==distance)
add(u[i], v[i], ); int ans = sap(start, end, n);
printf("%d\n", ans);
}
}
HDU3416 Marriage Match IV —— 最短路径 + 最大流的更多相关文章
- hdu3416 Marriage Match IV【最短路+最大流】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...
- HDU-3416 Marriage Match IV 最短路+最大流 找各最短路的所有边
题目链接:https://cn.vjudge.net/problem/HDU-3416 题意 给一个图,求AB间最短路的条数(每一条最短路没有重边.可有重复节点) 思路 首先把全部最短路的边找出来,再 ...
- hdu3416 Marriage Match IV 最短路+ 最大流
此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的 ...
- hdu3416 Marriage Match IV(最短路+网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意: 给出含n个点.m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有 ...
- 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 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- Q - Marriage Match IV (非重复最短路 + Spfa + 网络最大流Isap)
Q - Marriage Match IV Do not sincere non-interference. Like that show, now starvae also take part in ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- HDU3605:Marriage Match IV
Marriage Match IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- 【JQ同胞遍历】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 作业调度方案(codevs 1156)
题目描述 Description 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作, ...
- 从dataset表中获取某一列的所有值方法
原文发布时间为:2008-07-31 -- 来源于本人的百度文章 [由搬家工具导入] 可以datarow遍历所有行即可,如下:pubauthor这个表中的au_lname的所有值加到listbox上面 ...
- dos中定义变量与获取常见的引用变量以及四则运算、备份文件(set用法)
在dos中使用set定义变量: set a=8 (注意等号两边没有空格) 引用变量如: echo %a% 将打印a的值 (%a%是获取变量a的值) dos中 ...
- Xcode6 pch文件
XCode6里, 默认是没有pch文件的,如果我们想使用pch文件,需要手动添加,添加步骤如下 1.在XCode6中是么有pch文件的,如下图 2.创建pch文件 3.配置pch文件 ...
- docker-nginx-标记一下
拉取nginx镜像 然后启动容器: docker run -p : --name mynginx -d -v $PWD/nginx.conf:/etc/nginx/conf.d/default.con ...
- css3 画三角形
/*箭头向上*/ .arrow-up { width:0; height:0; border-left:20px solid transparent; border-right:20px solid ...
- WIP - 离散任务点击组件-错误:LOCATOR.CONTROL 的变元无效:ORG_LOCATOR_CONTROL=''
Getting Error "Invalid Argument to LOCATOR.CONTROL: ORG_LOCATOR_CONTROL='' in Material Requirem ...
- Intel Edison —— 控制GPIO接口,网页显示传感器数值
前言 原创文章,转载引用务必注明链接. 因为是使用Typora(markdown)写好然后复制到论坛的,推荐直接访问我的网站以获得更好地阅读体验. Intel XDK IoT 开发环境很久之前就上手了 ...
- mysqldbcopy 数据库复制工具
命令参考 mysqldbcopy --source=root:'xxxxxxx'@database s --destination=root:'^%xxxxxz'@databases orange:o ...