HDU3605:Marriage Match IV
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的更多相关文章
- HDU 3416:Marriage Match IV(最短路+最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意:给出n个点m条边,边信息分别是两个端点和一个费用,再给出一个起点和一个终点,问从起点到终点的完全不相 ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- HDU3416 Marriage Match IV —— 最短路径 + 最大流
题目链接:https://vjudge.net/problem/HDU-3416 Marriage Match IV Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- Q - Marriage Match IV (非重复最短路 + Spfa + 网络最大流Isap)
Q - Marriage Match IV Do not sincere non-interference. Like that show, now starvae also take part in ...
- Marriage Match IV(最短路+网络流)
Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 3081:Marriage Match II(二分图匹配+并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意:有n个男生n个女生,他们只有没有争吵或者女生a与男生A没有争吵,且女生b与女生a是朋友,因此女生b也 ...
- HDU 3416 Marriage Match IV dij+dinic
题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...
随机推荐
- 使用c++控制sqlite3
首先,到官网下载相关的压缩包 https://www.sqlite.org/download.html 但是要自己再重新编译一个, 博主自己收集了一下,密码:hixo https://pan.baid ...
- python核心编程2 第五章 练习
5-2 运算符(a) 写一个函数,计算并返回两个数的乘积(b) 写一段代码调用这个函数,并显示它的结果 def product(x, y): return x * y if __name__ == ' ...
- 第三课:PHP 语法
PHP 脚本在服务器上执行,然后向浏览器发送回纯 HTML 结果. 基础 PHP 语法 PHP 脚本可放置于文档中的任何位置. PHP 脚本以 <?php 开头,以 ?> 结尾: < ...
- C# 设置窗口大小为不可调、取消最大化、最小化窗口按键
this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节 this.MaximizeBox = false; //取消最大化按键 t ...
- Leetcode 606. 根据二叉树创建字符串
题目链接 https://leetcode.com/problems/construct-string-from-binary-tree/description/ 题目描述 你需要采用前序遍历的方式, ...
- PAT Basic 1057
1057 数零壹 给定一串长度不超过 105 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有 ...
- python里面list()函数
1. list() 方法用于将元组转换为列表. 元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中,列表是放于方括号中. 2. python里面的变量不用提前声明.
- The GNU C Library
Any Unix-like operating system needs a C library: the library which defines the ``system calls'' and ...
- runtime怎么添加属性、方法等
ivar表示成员变量 class_addIvar class_addMethod class_addProperty class_addProtocol class_replaceProperty
- 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(下)
随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容仍是关于Cortex-M3的基础内容,相 ...