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. 带搜索框的select下拉框

    利用select2制作带有搜索功能的select下拉框 1.引入线上css和js <link href="https://cdnjs.cloudflare.com/ajax/libs/ ...

  2. oracle中序列,同义词的创建

    序列 序列是用来生成唯一,连续的整数的数据库对象.序列通常用来自动生成主机那或唯一键的值.序列可以按升序排序, 也可以按降序排序.例如,销售流水表中的流水号可以使用序列自动生成. 创建序列语法: cr ...

  3. PHP siege 压测 QPS大小

    1.使用 PHP-FPM SOCKET的形式通讯 2.配置 PHP-FPM配置 [root@bogon php-fpm.d]# ls -al 总用量 drwxr-xr-x. root root 8月 ...

  4. Tomcat+jdk 环境处理 java jsp代码编写web环境的容器

    Tomcat是由 Apache 软件基金会下属的 Jakarta 项目开发的一个Servlet 容器,按照 SunMicrosystems 提供的技术规范,实现了对 Servlet 和 JavaSer ...

  5. .NET领域驱动设计系列(12)

    [.NET领域驱动设计实战系列]专题十一:.NET 领域驱动设计实战系列总结 摘要: 一.引用 其实在去年本人已经看过很多关于领域驱动设计的书籍了,包括Microsoft .NET企业级应用框架设计. ...

  6. 查找并绘制轮廓 opencv

    findContours(): 第二个参数为一个检测到的轮廓,函数调用后的运算结果都放在这里,每个轮廓存储为1个点向量,用point类型的vector表示. 第三个参数表示轮廓数量,包含了许多元素.每 ...

  7. 笔记-python-standard library-16.3 time

    笔记-python-standard library-16.3 time 1.      time 1.1.    开始 time模块中时间表现的格式主要有三种: timestamp时间戳,时间戳表示 ...

  8. 关于修改zeppelin的代码显示

    最近我在修改zeppelin(0.7版本)的源码相关的知识,目前做的工作是修改zeppelin的代码,为了让zeppelin可以可以在页面中显示数据集,并且在其数据库中存储式真实的路径1.如果我们要运 ...

  9. Eclipse 窗口说明---Eclipse教程第03课

    Eclipse 工作台(Workbench) 首先,让我们来看一下Eclipse 作台用户界面,和它里面的各种组件. 工作台是多个窗口的集合.每个窗口包含菜单栏,工具栏,快捷方式栏,以及一个或者多个透 ...

  10. VSX-1 概述

    博客搁置了一段时间,一直想写,无从下手,正好最近在做VS2010扩展方面的项目,所以写VSX系列文章以记之. 背景 现有工作是做金融行业,主要项目是一套银行综合前端系统,也就是银行平时用的最多的一个系 ...