Marriage Match IV(最短路径+最大流)

2023-11-08 11:38

本文主要是介绍Marriage Match IV(最短路径+最大流),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

题目大意:求最短路的个数

题目思路:先用SPFA求出最短路径,然后用SAP算法求出最大网络流,即最短的个数,以最短路径上的边建图,用邻接链表存边

题目:

Marriage Match IV

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


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 76 7 1 2 1 2 3 1 1 3 3 3 4 1 3 5 1 4 6 1 5 6 1 1 62 2 1 2 1 1 2 2 1 2

Sample Output
  
2 1 1
#include <iostream>
#include <queue>
#include <vector>
#include<string.h>
#include<stdio.h>
using namespace std;
const int inf=0x3f3f3f3f;
struct rec
{int u,v,w,link;
} edge[300005];
struct re
{int v,c;
};
vector<re> E[1005];
bool vis[1005];
int head[1005],pre[1005],cur[1005],dis[1005],gap[1005];
int n,m,num,st,ed;
inline void addedge(int u,int v,int w)
{edge[num].u=u;edge[num].v=v;edge[num].w=w;edge[num].link=head[u];head[u]=num++;edge[num].u=v;edge[num].v=u;edge[num].w=0;edge[num].link=head[v];head[v]=num++;
}
void SPFA()
{int i,v,u;memset(vis,0,sizeof(vis));for(i=1; i<=n; i++)dis[i]=inf;queue<int> Q;dis[st]=0;Q.push(st);while(!Q.empty()){u=Q.front();Q.pop();vis[u]=0;for(i=0; i<E[u].size(); i++){v=E[u][i].v;if(dis[v]>dis[u]+E[u][i].c){dis[v]=dis[u]+E[u][i].c;if(!vis[v]){vis[v]=1;Q.push(v);}}}}
}
int max_flow()
{int i,k,u,v,aug=inf,ans=0;memset(dis,0,sizeof(dis));memset(gap,0,sizeof(gap));gap[0]=n;u=pre[st]=st;for(i=1; i<=n; i++)cur[i]=head[i];while(dis[st]<n){
loop:for(i=cur[u]; i; i=cur[u]=edge[i].link){v=edge[i].v;if(dis[u]==dis[v]+1 && edge[i].w){pre[v]=u;u=v;if(aug>edge[i].w)aug=edge[i].w;if(v==ed){for(u=pre[v]; v!=st; v=u,u=pre[u]){edge[cur[u]].w-=aug;edge[cur[u]^1].w+=aug;}ans+=aug;aug=inf;}goto loop;}}k=n;for(i=head[u]; i; i=edge[i].link){v=edge[i].v;if(k>dis[v] && edge[i].w){cur[u]=i;k=dis[v];}}if(--gap[dis[u]]==0)break;gap[dis[u]=k+1]++;u=pre[u];}return ans;
}
int main()
{int T,i,j,u,v,c;re tmp;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(i=1; i<=n; i++)E[i].clear();num=2;memset(head,0,sizeof(head));for(i=1; i<=m; i++){scanf("%d%d%d",&u,&v,&c);if(u==v)continue;tmp.v=v;tmp.c=c;E[u].push_back(tmp);}scanf("%d%d",&st,&ed);SPFA();for(i=1; i<=n; i++)for(j=0; j<E[i].siz e(); j++)if(dis[E[i][j].v]==dis[i]+E[i][j].c)addedge(i,E[i][j].v,1);printf("%d\n",max_flow());}return 0;
}



这篇关于Marriage Match IV(最短路径+最大流)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369599

相关文章

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

python之流程控制语句match-case详解

《python之流程控制语句match-case详解》:本文主要介绍python之流程控制语句match-case使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录match-case 语法详解与实战一、基础值匹配(类似 switch-case)二、数据结构解构匹

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

如何提高Redis服务器的最大打开文件数限制

《如何提高Redis服务器的最大打开文件数限制》文章讨论了如何提高Redis服务器的最大打开文件数限制,以支持高并发服务,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录如何提高Redis服务器的最大打开文件数限制问题诊断解决步骤1. 修改系统级别的限制2. 为Redis进程特别设置限制

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl