Marriage Match IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6230    Accepted Submission(s): 1804

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416

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

题意:

一个男生要去到一个城市找女生,他们两个的城市间有多条路径,现在他肯定想走最短路啦~

问他能走多少条最短路到女生的城市,并且要求这些最短路路径不能重复。

题解:

最近事情有点多,很久没更新博客了...

这题一开始我想的是费用流,但是T了。

正解是最大流,但是是经过“改造后”的最大流。这个最大流网络应该是由容量为1的,在原图最短路径上的边构成,最后我们跑个Dinic就行了。

要找最短路径上面的边,我们从起点spfa一次,再从终点spfa一次就行了。注意跑spfa建边的时候不要建双向边,我一开始就是这里WA了...

具体做法见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1<<30
using namespace std;
typedef long long ll;
const int N =, M = 1e5+;
int n,m,s,t,T,mx;
int d1[N],d2[N],d[N],vis[N],head[N];
pair <pair<int,int>,int> E[M];
struct Edge{
int v,next,c,w;
}e[M<<];
int tot;
void adde(int u,int v,int c,int w){
e[tot].v=v;e[tot].c=c;e[tot].next=head[u];e[tot].w=w;head[u]=tot++;
}
void spfa(int S){
queue <int> q;q.push(S);
memset(vis,,sizeof(vis));vis[S]=;
for(int i=;i<=n;i++) d2[i]=INF;d2[S]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d2[v]>d2[u]+e[i].w){
d2[v]=d2[u]+e[i].w;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
}
int bfs(){
memset(d,,sizeof(d));d[s]=;
queue <int > q;q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(e[i].c> && !d[v]){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[t]!=;
}
int dfs(int u,int a){
if(u==t || a==) return a;
int flow=,f;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[u]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f>){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[u]=-;
return flow;
}
int Dinic(){
int flow=;
while(bfs()) flow+=dfs(s,INF);
return flow;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
tot=;memset(head,-,sizeof(head));
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
E[i]=make_pair(make_pair(u,v),w);
adde(u,v,,w);
}
scanf("%d%d",&s,&t);
spfa(s);
for(int i=;i<=n;i++) d1[i]=d2[i];
tot=;memset(head,-,sizeof(head));
for(int i=;i<=m;i++){
int u=E[i].first.first,v=E[i].first.second,w=E[i].second;
adde(v,u,,w);
}
spfa(t);
mx=d1[t];
tot=;memset(head,-,sizeof(head));
for(int i=;i<=m;i++){
int u =E[i].first.first,v=E[i].first.second,w=E[i].second;
if(d1[u]+d2[v]+w==mx){
adde(u,v,,INF);
adde(v,u,,INF);
}
}
printf("%d\n",Dinic());
}
return ;
}

HDU3605:Marriage Match IV的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意:给出n个点m条边,边信息分别是两个端点和一个费用,再给出一个起点和一个终点,问从起点到终点的完全不相 ...

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

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

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

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

  4. HDU3416 Marriage Match IV —— 最短路径 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3416 Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    ...

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

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

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

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

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

    Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Othe ...

  8. HDU 3081:Marriage Match II(二分图匹配+并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意:有n个男生n个女生,他们只有没有争吵或者女生a与男生A没有争吵,且女生b与女生a是朋友,因此女生b也 ...

  9. HDU 3416 Marriage Match IV dij+dinic

    题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...

随机推荐

  1. 在React Native中集成热更新

    最近,在项目DYTT集成了热更新,简单来说,就是不用重新下载安装包即可达到更新应用的目的,也不算教程吧,这里记录一下. 1.热更新方案 目前网上大概有两个比较广泛的方式,分别是 react-nativ ...

  2. 提高mysql性能(like搜索替换 )

    一 .mysql用find_in_set代替like搜索提高性能 SELECT * from mobantestinfo1 where find_in_set('33',info2); 二 .使用内部 ...

  3. CSS计数器(自定义列表)

    概念 CSS3计数器(CSS Counters)可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能(自定义有序列表) 与有序列表相比,它的突出特性在于可以对任意元素计数,同时实 ...

  4. 通俗版解释网关,IP地址,ARP欺骗,DDOS攻击

    计算机主机网关的作用是什么? 假设你的名字叫小不点,你住在一个大院子里,你的邻居有很多小伙伴,在门口传达室还有个看大门的李大爷,李大爷就是你的网关.当你想跟院子里的某个小伙伴玩,只要你在院子里大喊一声 ...

  5. 内存释放free函数的异常问题

    本次在实际应用中遇到一个问题,首先是定义了一个指针,然后这个指针指向某一个地址,但是这个地址不是用malloc分配的.如果后面用free去释放这个指针会产生什么现象. 首先看下指针的声明和使用 uin ...

  6. ios交叉编译dylib

    ios交叉编译dylib 因多个静态库,libes,libffmpeg,libmt. libpcap 使用不方便 在封装一层接口,生成动态库(c代码),由IOS app上层调用. IOS_BASE_S ...

  7. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

  8. <<程序猿健康指南>> 笔记

    序言: 长时间对着电脑工 作.一天下来基本不怎么走动的人,患高血压及 2 型糖尿病的风险远高于其他人群, 这两种疾病会对人体的健康产生长久的严重影响,还会增加心脏病及中风的几率. 前言: 阿米什人(A ...

  9. Visual Studio 2017 的 JavaScript 调试功能的关闭

    关闭方法其实很简单,Options => Debugging => General => Enable JavaScript debugging for ASP.NET (Chrom ...

  10. Pascal小游戏 俄罗斯方块

    俄罗斯方块已经成为了和“Hello World”一样的程序了吧? 不要直接复制,可能需要事先 Format. program cube;uses crt,graph,dos;var gd,gm:sma ...