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. MySQL - Linux下安装

    本安装方式仅对5.7.21版本负责. 下载地址:wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2 ...

  2. ubuntu18.04.1LTS系统远程工具secureCRT

    ubuntu18.04.1LTS类windows的系统下安装远程管理工具 本地电脑之前安装的是win10,疲于win10频繁的更新和各种兼容问题,果断放弃win10系统,安装了Ubuntu 18.04 ...

  3. 《Redis设计与实现》- AOF持久化

    1. AOF持久化 Redis AOF 持久化是通过保存Redis服务器所执行的写命令来记录数据库状态的. 2. RDB持久化与AOF持久化的区别 RDB持久化 RDB持久化通过保存数据中的键值对来记 ...

  4. Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)

    出现  Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)时在php.ini文件中配置 memor ...

  5. Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/config/springdemo-config.xml]

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...

  6. ORA-01122: 数据库文件 1 验证失败

    1.SQL>shutdown abort 如果数据库是打开状态,强行关闭 2.SQL>sqlplus / as sysdba 3.SQL>startupORACLE 例程已经启动. ...

  7. centos使用--软件启动关闭等操作的命令

    关于centos服务的命令经常会遇到这几种 service systemctl /etc/init.d/ 1 service和/etc/init.d/ service是一个脚本命令,分析service ...

  8. MyEclipse - 问题集 - maven update project 后,项目jdk的版本变化

    解决方法: 进入maven安装根目录,conf/settings.xml <profiles> <profile> <id>jdk-1.7</id> & ...

  9. FMDB的线程安全

    最近面试被问到FMDB的多线程处理问题,因为之前项目中是移植别人的代码,没有踩过这里的坑. 问题: 多线程同时访问数据库时,报数据库锁定的问题,错误信息是: Unknown error finaliz ...

  10. 程序第一次启动推送跳转,handleOpenURL没法跳转的原因

    iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...