Marriage Match IV

题目链接:

http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q

Description


Do not sincere non-interference。
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


The first line is an integer T indicating the case number.(1

Output


Output a line with a integer, means the chances starvae can get at most.

Sample Input


```
3
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

</big>

##Sample Output
<big>
2
1
1
</big> ##Hint
<big>
</big> <br/>
##题意:
<big>
求最短路的条数.
要求这些路径互相没有相同的边.
</big> <br/>
##题解:
<big>
由于要求同一条边不能出现在两条最短路中.
所以直接标记出最短路中的边,对这些边跑一次最大流即可. (这里直接用了sap模版)
<br/>
若这个题没有不共边的要求,就直接对最短路中的边dfs即可.
[HDU1142-A Walk Through the Forest](http://acm.hdu.edu.cn/showproblem.php?pid=1142)
题解:http://www.cnblogs.com/Sunshine-tcf/p/5752205.html
</big> <br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 501000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n, m;
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > q;
bool vis[maxn];
int edges, u[maxn], v[maxn], w[maxn];
int first[maxn], _next[maxn];
int dist[maxn];
int pre1[maxn]; void add_edge(int s, int t, int val) {
u[edges] = s; v[edges] = t; w[edges] = val;
_next[edges] = first[s];
first[s] = edges++;
} void dijkstra(int s) {
memset(pre1, -1, sizeof(pre1));
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) dist[i]=inf; dist[s] = 0;
while(!q.empty()) q.pop();
q.push(make_pair(dist[s], s)); while(!q.empty()) {
pii cur = q.top(); q.pop();
int p = cur.second;
if(vis[p]) continue; vis[p] = 1;
for(int e=first[p]; e!=-1; e=_next[e]) if(dist[v[e]] > dist[p]+w[e]){
dist[v[e]] = dist[p] + w[e];
q.push(make_pair(dist[v[e]], v[e]));
pre1[v[e]] = p;
}
}
} //最大流SAP
struct Node {
int to,_next,cap;
}edge[maxn];
int tol;
int head[maxn];
int gap[maxn],dis[maxn],pre[maxn],cur[maxn];
void init() {
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0) {
edge[tol].to=v;edge[tol].cap=w;edge[tol]._next=head[u];head[u]=tol++;
edge[tol].to=u;edge[tol].cap=rw;edge[tol]._next=head[v];head[v]=tol++;
} int sap(int start,int end,int nodenum)
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memcpy(cur,head,sizeof(head));
int u=pre[start]=start,maxflow=0,aug=-1;
gap[0]=nodenum;
while(dis[start]<nodenum)
{
loop:
for(int &i=cur[u];i!=-1;i=edge[i]._next)
{
int v=edge[i].to;
if(edge[i].cap&&dis[u]==dis[v]+1)
{
if(aug==-1||aug>edge[i].cap)
aug=edge[i].cap;
pre[v]=u;
u=v;
if(v==end)
{
maxflow+=aug;
for(u=pre[u];v!=start;v=u,u=pre[u])
{
edge[cur[u]].cap-=aug;
edge[cur[u]^1].cap+=aug;
}
aug=-1;
}
goto loop;
}
}
int mindis=nodenum;
for(int i=head[u];i!=-1;i=edge[i]._next)
{
int v=edge[i].to;
if(edge[i].cap&&mindis>dis[v])
{
cur[u]=i;
mindis=dis[v];
}
}
if((--gap[dis[u]])==0)break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
} int main(void)
{
//IN; int t; cin >> t; int ca = 1;
while(t--)
{
memset(first, -1, sizeof(first)); edges = 0;
cin >> n >> m; while(m--) {
int u,v,w; scanf("%d %d %d", &u, &v, &w);
if(u == v) continue;
add_edge(u, v, w);
}
int s, t; cin >> s >> t; dijkstra(s); init();
for(int e=0; e<edges; e++) {
//判断是否是最短路上的边
if(dist[v[e]] == dist[u[e]]+w[e]) {
addedge(u[e], v[e], 1);
}
} int paths = sap(s, t, n); printf("%d\n", paths);
} return 0;
}

HDU 3416 Marriage Match IV (求最短路的条数,最大流)的更多相关文章

  1. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

  2. HDU 3416 Marriage Match IV(最短路,网络流)

    题面 Do not sincere non-interference. Like that show, now starvae also take part in a show, but it tak ...

  3. HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】

    <题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...

  4. hdu 3416 Marriage Match IV 【 最短路 最大流 】

    求边不可重复的最短路条数 先从起点到终点用一次dijkstra,再从终点到起点用一次dijkstra,来判断一条边是否在最短路上 如果在,就将这条边的两个端点连起来,容量为1 再跑一下dinic(), ...

  5. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  6. HDU 3416 Marriage Match IV(ISAP+最短路)题解

    题意:从A走到B,有最短路,问这样不重复的最短路有几条 思路:先来讲选有效边,我们从start和end各跑一次最短路,得到dis1和dis2数组,如果dis1[u] + dis2[v] + cost[ ...

  7. HDU 3416 Marriage Match IV (最短路建图+最大流)

    (点击此处查看原题) 题目分析 题意:给出一个有n个结点,m条单向边的有向图,问从源点s到汇点t的不重合的最短路有多少条,所谓不重复,意思是任意两条最短路径都不共用一条边,而且任意两点之间的边只会用一 ...

  8. HDU 3416 Marriage Match IV (Dijkstra+最大流)

    题意:N个点M条边的有向图,给定起点S和终点T,求每条边都不重复的S-->T的最短路有多少条. 分析:首先第一步需要找出所有可能最短路上的边.怎么高效地求出呢?可以这样:先对起点S,跑出最短路: ...

  9. HDU 3416 Marriage Match IV

    最短路+最大流 #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

随机推荐

  1. 内核MKDEV(MAJOR, MINOR)宏

    版本:linux-2.6.24.4宏:    MKDEV(MAJOR, MINOR);  说明: 获取设备在设备表中的位置.        MAJOR   主设备号        MINOR   次设 ...

  2. mysql定时计划任务,ON COMPLETION [NOT] PRESERVE 当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而声明PRESERVE的作用是使事件在执行完毕后不会被Drop掉

    当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当 ...

  3. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...

  4. hdu 4143 A Simple Problem (变形)

    题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n),  y = (a+b)/ ...

  5. next_permutation()函数 和 prev_permutation() 按字典序求全排列

    next_permutation功能:    求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...

  6. iOS开发:在Xcode中用Pods管理第三方库

    之前写了一篇 iOS开发:在Swift中调用oc库 ,今天记录一下如何用Pods的方式来管理第三方库,包括Swift/Object-C的库. 在这之前请先查阅Guides.CocoaPods如何使用的 ...

  7. UVa 11389 (贪心) The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...

  8. 图表框架HelloCharts(1)线形图

    效果图 1. 导入 .aar 2. fragment_line_chart.xml <RelativeLayout xmlns:android="http://schemas.andr ...

  9. Java Web编程的主要组件技术——Struts入门

    参考书籍:<J2EE开源编程精要15讲> Struts是一个开源的Java Web框架,很好地实现了MVC设计模式.通过一个配置文件,把各个层面的应用组件联系起来,使组件在程序层面联系较少 ...

  10. 【 D3.js 高级系列 — 7.0 】 标注地点

    有时需要告诉用户地图上的一些目标,如果该目标是只需要一个坐标就能表示的,称其为"标注". 1. 标注是什么 标注,是指地图上只需要一个坐标即可表示的元素.例如,在经纬度(116, ...