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

时间:2022-12-29 21:23:04

Marriage Match IV

http://acm.hdu.edu.cn/showproblem.php?pid=3416

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

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
 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define maxn 100005
#define MAXN 10005
#define mem(a,b) memset(a,b,sizeof(a))
const int N=;
const int M=;
const int INF=0x3f3f3f3f;
using namespace std;
inline int read()
{
char ch=' ';
int ans=;
while(ch<'' || ch>'')
ch=getchar();
while(ch<='' && ch>='')
{
ans=ans*+ch-'';
ch=getchar();
}
return ans;
}
int n;
vector<pair<int,int> >ve[maxn];
int dis[maxn],dis1[maxn],dis2[maxn],vis[maxn];
int a[maxn],b[maxn],c[maxn];
struct sair{
int pos,len;
friend bool operator<(sair a,sair b){
return a.len>b.len;
}
}; void Dijstra(int s){
for(int i=;i<=n;i++){
dis[i]=INF;
vis[i]=;
}
priority_queue<sair>Q;
sair tmp;
tmp.pos=s,tmp.len=;
Q.push(tmp);
dis[s]=;
int now,Ne,len;
while(!Q.empty()){
tmp=Q.top();
Q.pop();
now=tmp.pos;
if(!vis[now]){
vis[now]=;
for(int i=;i<ve[now].size();i++){
Ne=ve[now][i].first;
len=ve[now][i].second;
if(dis[Ne]>dis[now]+len){
dis[Ne]=dis[now]+len;
tmp.pos=Ne;
tmp.len=dis[Ne];
Q.push(tmp);
}
}
}
}
} struct Edge{
int v,next;
int cap,flow;
}edge[MAXN*];//注意这里要开的够大。。不然WA在这里真的想骂人。。问题是还不报RE。。
int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
int cnt=;//实际存储总边数
void isap_init()
{
cnt=;
memset(pre,-,sizeof(pre));
}
void isap_add(int u,int v,int w)//加边
{
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void add(int u,int v,int w){
isap_add(u,v,);
isap_add(v,u,);
}
bool bfs(int s,int t)//其实这个bfs可以融合到下面的迭代里,但是好像是时间要长
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
dep[t]=;
queue<int>q;
while(!q.empty())
q.pop();
q.push(t);//从汇点开始反向建层次图
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=pre[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(dep[v]==-&&edge[i^].cap>edge[i^].flow)//注意是从汇点反向bfs,但应该判断正向弧的余量
{
dep[v]=dep[u]+;
gap[dep[v]]++;
q.push(v);
//if(v==sp)//感觉这两句优化加了一般没错,但是有的题可能会错,所以还是注释出来,到时候视情况而定
//break;
}
}
}
return dep[s]!=-;
}
int isap(int s,int t)
{
if(!bfs(s,t))
return ;
memcpy(cur,pre,sizeof(pre));
//for(int i=1;i<=n;i++)
//cout<<"cur "<<cur[i]<<endl;
int u=s;
path[u]=-;
int ans=;
while(dep[s]<n)//迭代寻找增广路
{
if(u==t)
{
int f=INF;
for(int i=path[u];i!=-;i=path[edge[i^].v])//修改找到的增广路
f=min(f,edge[i].cap-edge[i].flow);
for(int i=path[u];i!=-;i=path[edge[i^].v])
{
edge[i].flow+=f;
edge[i^].flow-=f;
}
ans+=f;
u=s;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
{
cur[u]=path[v]=i;//当前弧优化
flag=true;
break;
}
}
if(flag)
{
u=v;
continue;
}
int x=n;
if(!(--gap[dep[u]]))return ans;//gap优化
for(int i=pre[u];i!=-;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
{
x=dep[edge[i].v];
cur[u]=i;//常数优化
}
}
dep[u]=x+;
gap[dep[u]]++;
if(u!=s)//当前点没有增广路则后退一个点
u=edge[path[u]^].v;
}
return ans;
} int main(){
int T;
T=read();
while(T--){
int m;
n=read();
m=read();
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=m;i++){
a[i]=read();
b[i]=read();
c[i]=read();
}
int s,t;
s=read();
t=read();
for(int i=;i<=m;i++){
if(a[i]!=b[i])
ve[a[i]].push_back(make_pair(b[i],c[i]));
}
Dijstra(s);
memcpy(dis1,dis,sizeof(dis));
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=m;i++){
if(a[i]!=b[i])
ve[b[i]].push_back(make_pair(a[i],c[i]));
}
Dijstra(t);
memcpy(dis2,dis,sizeof(dis));
isap_init();
for(int i=;i<=m;i++){
if(a[i]!=b[i]&&dis1[a[i]]+dis2[b[i]]+c[i]==dis1[t]){
add(a[i],b[i],);
}
}
int ans=;
ans=isap(s,t);
printf("%d\n",ans);
}
}

Marriage Match IV(最短路+网络流)的更多相关文章

  1. HDU 3416 Marriage Match IV &lpar;最短路建图&plus;最大流)

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

  2. hdu3416 Marriage Match IV 最短路&plus; 最大流

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

  3. HDU-3416 Marriage Match IV 最短路&plus;最大流 找各最短路的所有边

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

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

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

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

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

  6. hdu 3416 Marriage Match IV (最短路&plus;最大流)

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

  7. Q - Marriage Match IV &lpar;非重复最短路 &plus; Spfa &plus; 网络最大流Isap&rpar;

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

  8. HDU3605:Marriage Match IV

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

  9. HDU3416 Marriage Match IV —— 最短路径 &plus; 最大流

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

随机推荐

  1. C&num;面向对象设计模式纵横谈——3&period;Abstract Factory 抽象工厂&lpar;创建型模式&rpar;

    动机(Motivation) 在软件系统中经常面临着“一系列相互依赖的对象”的创建工作,同时,由于需求变化,往往存在更多系列对象的创建工作.如何应对这种变化?如何绕过常规对象的创建,提供一种“封装机制 ...

  2. 注意SQLServer2012中带参数的XP&lowbar;ReadErrorLog

    --15:26 2014-6-10数据库错误日志通知其中有一部分是检测ERRORLOG中若有用户登录失败信息时,会记录到Errorlog表,并邮件提醒.当时直接从同事那拿过来的脚本(08),按理说版本 ...

  3. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

    例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php function getExt($url){ $arr=parse_url( ...

  4. BZOJ 3097&colon; Hash Killer I【构造题,思维题】

    3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 963  Solved: 36 ...

  5. ubuntu 安装 evpp

    ubuntu 安装 evpp 来源 https://www.cnblogs.com/wisdomyzw/p/9402440.html Ubuntu虚拟机安装开源库evpp说明: EVPP为奇虎360基 ...

  6. 基于emWin的WAV,MP3软解软件播放器,带类似千千静听频谱,含uCOS-III和FreeRTOS两个版本

    第9期:WAV,MP3软解播放器,带类似千千静听频谱配套例子:V6-916_STemWin提高篇实验_WAV,MP3软解播放器,带类似千千静听频谱(uCOS-III)V6-917_STemWin提高篇 ...

  7. rpc轻量级框架实例

  8. &lbrack;程序员的业余生活&rsqb;一周读完《高效能人士的七个习惯》Day1&colon;这是不是一碗鸡汤?

    提出问题 今天突然想聊聊最近对职场的一些感悟. 这段时间,小端一直在思考一个问题:作为一个程序员,怎么才能成为团队的核心? 还记得刚入职场那几年,小端一直觉得,技术过硬,经验丰富,敢打敢拼,就是答案. ...

  9. Jmeter实例&lpar;一&rpar;&lowbar;Beanshell脚本断言Mock接口

    我们在做接口断言的时候,如果遇到复杂的json,可以考虑用beanshell脚本去解析list,同时加入自定义的断言 Mock例:https://www.easy-mock.com/mock/5cb4 ...

  10. sdoi2017苹果树

    题解: 非常奇妙的一题.. 没有免费操作我都不会$nk$....考试打个暴力就可以走人了 树上有依赖背包问题的正确做法是(为啥我之前学的不是这样的啊) 按照后续遍历做背包 做到一个点的时候 枚举它选不 ...