Marriage Match IV

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

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
 

Author

starvae@HDU
 

Source

 

只有最短路上的边才加入网络,容量都为1,跑最大流

 //2017-08-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[M]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} struct Dinic{
int level[N], S, T;
void init(){
tot = ;
memset(head, -, sizeof(head));
}
void set_s_t(int _S, int _T){
S = _S;
T = _T;
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
}
}dinic; //dis[0][u]表示从起点到u的最短距离,dis[1][u]表示从终点到u的最短距离,cnt[u]记录u入队次数,判负环用
int dis[][N], cnt[N];
bool vis[N];
bool spfa(int s, int n, int op){
memset(vis, false, sizeof(vis));
memset(dis[op], INF, sizeof(dis));
memset(cnt, , sizeof(cnt));
vis[s] = true;
dis[op][s] = ;
cnt[s] = ;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(dis[op][v] > dis[op][u] + w){
dis[op][v] = dis[op][u] + w;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} struct Line{
int u, v, w;
}line[M]; int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputO.txt", "r", stdin);
int T, n, m, A, B;
cin>>T;
while(T--){
cin>>n>>m;
int u, v, w;
for(int i = ; i < m; i++)
cin>>line[i].u>>line[i].v>>line[i].w;
cin>>A>>B;
dinic.init();
for(int i = ; i < m; i++)
add_edge(line[i].u, line[i].v, line[i].w);
spfa(A, n, );//求从起点开始到各个点的最短路
dinic.init();
for(int i = ; i < m; i++)
add_edge(line[i].v, line[i].u, line[i].w);
spfa(B, n, );//求从终点开始到各个点的最短路
dinic.init();
dinic.set_s_t(A, B);
for(int i = ; i < m; i++){
u = line[i].u;
v = line[i].v;
w = line[i].w;
//只有最短路上的边才加入网络
if(dis[][u]+dis[][v]+w == dis[][B]){
add_edge(u, v, );
add_edge(v, u, );
}
}
cout<<dinic.maxflow()<<endl;
}
return ;
}

HDU3416(KB11-O spfa+最大流)的更多相关文章

  1. bzoj 2285 [Sdoi2011]保密(二分,spfa + 最大流)

    Description 现在,保密成为一个很重要也很困难的问题.如果没有做好,后果是严重的.比如,有个人没有自己去修电脑,又没有拆硬盘,后来的事大家都知道了. 当然,对保密最需求的当然是军方,其次才是 ...

  2. POJ 2175 spfa费用流消圈

    题意:给出n栋房子位置和每栋房子里面的人数,m个避难所位置和每个避难所可容纳人数.然后给出一个方案,判断该方案是否最优,如果不是求出一个更优的方案. 思路:很容易想到用最小费用流求出最优时间,在与原方 ...

  3. 最长可重区间集 spfa费用流

    给定实直线L上的n个开区间,和一个正整数k 选取若干个区间,在保证实直线L上的任意一个点最多被选出区间覆盖k次的情况下,使得这些区间的长度和最大 先把区间按照左端点排序, 考虑到重复其实就代表着相交, ...

  4. ZOJ 3362 Beer Problem(SPFA费用流应用)

    Beer Problem Time Limit: 2 Seconds      Memory Limit: 32768 KB Everyone knows that World Finals of A ...

  5. hdu3416+hdu6582(最短路+最大流)

    题意 hdu3416: 给一个图,边不能重复选,问有多少个最短路 hdu6582: 给一个图,问最少删除边权多少的边后,最短路长度增加 分析 边不能重复选这个条件可以想到边权为1,跑最大流,所以我们可 ...

  6. Spfa费用流模板

    ; ,maxm=; ,fir[maxn],nxt[maxm],to[maxm]; int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int ...

  7. 洛谷.4015.运输问题(SPFA费用流)

    题目链接 嗯..水题 洛谷这网络流二十四题的难度评价真神奇.. #include <queue> #include <cstdio> #include <cctype&g ...

  8. BZOJ.4819.[SDOI2017]新生舞会(01分数规划 费用流SPFA)

    BZOJ 洛谷 裸01分数规划.二分之后就是裸最大费用最大流了. 写的朴素SPFA费用流,洛谷跑的非常快啊,为什么有人还T成那样.. 当然用二分也很慢,用什么什么迭代会很快. [Update] 19. ...

  9. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

随机推荐

  1. jzoj4235 序列

    取前50個數暴力即可 #include<bits/stdc++.h> using namespace std; int n,m,a[100010],q[5]; int main(){ sc ...

  2. 在cad2008引用了错误的com接口的dll导致出现了

    请求“System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, Publi ...

  3. D02-R语言基础学习

    R语言基础学习——D02 20190423内容纲要: 1.前言 2.向量操作 (1)常规操作 (2)不定长向量计算 (3)序列 (4)向量的删除与保留 3.列表详解 (1)列表的索引 (2)列表得元素 ...

  4. Network - Tips

    001 - 查询whois 可通过在线工具进行查询: https://www.whois365.com http://whois.aliyun.com http://whois.chinaz.com ...

  5. 【learning】 扩展lucas定理

    首先说下啥是lucas定理: $\binom n m \equiv \binom {n\%P} {m\%P} \times \binom{n/P}{m/P} \pmod P$ 借助这个定理,求$\bi ...

  6. [Leetcode]146.LRU缓存机制

    Leetcode难题,题目为: 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key ...

  7. How to change windows applicatioin's position via Win32 API

    可以使用的Win32 API是: [DllImport("user32.dll")] private extern static bool SetWindowPos(IntPtr ...

  8. vector源码1(参考STL源码--侯捷):源码

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  9. JAVA框架之Hibernate【Hibernate缓存详解】

    1.缓存介绍 Hibernate中提供了两级Cache,第一级别的缓存是Session级别的缓存,它是属于事务范围的缓存.这一级别的缓存由hibernate管理的,一般情况下无需进行干预:第二级别的缓 ...

  10. 自定义 Scrapy 爬虫请求的 URL

    之前使用 scrapy 抓取数据的时候 ,默认是在逻辑中判断是否执行下一次请求 def parse(self): # 获取所有的url,例如获取到urls中 for url in urls: yiel ...