题目链接: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

Problem 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<=T<=65)
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
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

 
Sample Output
2
1
1
 
Author
starvae@HDU
 
Source

题意:

求有多少条最短路径,要求边不能够重叠(但题目可以有重边,即两个点之间可以有几条边)。

题解:

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 —— 最短路径 + 最大流的更多相关文章

  1. hdu3416 Marriage Match IV【最短路+最大流】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html   ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...

  2. HDU-3416 Marriage Match IV 最短路+最大流 找各最短路的所有边

    题目链接:https://cn.vjudge.net/problem/HDU-3416 题意 给一个图,求AB间最短路的条数(每一条最短路没有重边.可有重复节点) 思路 首先把全部最短路的边找出来,再 ...

  3. hdu3416 Marriage Match IV 最短路+ 最大流

    此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的 ...

  4. hdu3416 Marriage Match IV(最短路+网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意: 给出含n个点.m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有 ...

  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 (最短路+最大流)

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

  7. Q - Marriage Match IV (非重复最短路 + Spfa + 网络最大流Isap)

    Q - Marriage Match IV Do not sincere non-interference. Like that show, now starvae also take part in ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. HDU3605:Marriage Match IV

    Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. linux 安装软件出现/tmp 磁盘不足时 解决方案

    1.解决办法 mkdir  文件夹——你可以使用的文件夹 比如说 mkdir /mnt/tmp 然后只要export TMPDIR=/mnt/tmp 这样就不会出现 tmp文件夹不够用的情况

  2. *Codeforces891E. Lust

    $n \leq 5000$的数列,$k \leq 1e9$次操作,每次随机选一个数-1,然后把其他数的积加入答案.问最后答案期望,$mod \ \ 1e9+7$. 略微观察可以发现答案=初始数列的积- ...

  3. shell的case脚本的简单入门

    shell的case脚本的简单入门 示例1: #/bin/bash a=$ case "$a" in ") echo 'hell 2';; ") echo 'h ...

  4. (10) android控件-date

    1.TimePicker <TimePicker android:id="@+id/timePicker4" android:layout_width="wrap_ ...

  5. 如何稳定地使用 Google 搜索https://encrypted.google.com/

    方法很简单.用记事本打开 hosts 文件(Windows Vista 和 Windows 7 用户请先使用管理员权限打开记事本,然后将 hosts 文件拖进记事本中),在最下面添加如下内容: 203 ...

  6. 【flyway】开源的数据库版本管理工具【migration】

    开源的数据库版本管理工具[migration] 记录

  7. 若菜acmer感觉自己智商全然被碾压了QAQ~~

    题目大意是:输入n,m,给出n*m(n.m<=100)的不是正规的布满棋子的棋盘,求最少改几个棋子能够使得棋盘正规,正规的棋盘必须是每一个相邻的棋子颜色都不同(仅仅有黑白两种,用0,1取代) 比 ...

  8. weex 项目开发(六)weexpack 项目 打包、签名、发布

    一. weexpack build android  和  weexpack run android 的 区别. (1)单纯打包 weexpack build android (2)打包并运行 wee ...

  9. reorder-list——链表、快慢指针、逆转链表、链表合并

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  10. Yii 清理缓存

    html: <button onclick="clearCache()">ClearCache</button> js: function clearCac ...