HDU 3416
Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2470 Accepted Submission(s): 742
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?
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.
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
2
1
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define N 1010
#define M 101000
const int INF = 1000000000; int n, m, sp, tp, cnte, cnte2, head[N], lowdis[N], head2[N], dep[N], tail[N];
bool vis[N]; class Edge
{
public:
int u, v, w, next, next2;
};
Edge edge1[M*2], edge2[M*2]; void addEdge1(int u, int v, int w)
{
edge1[cnte].u = u; edge1[cnte].v = v;
edge1[cnte].w = w; edge1[cnte].next = head[u];
head[u] = cnte;
edge1[cnte].next2 = tail[v];
tail[v] = cnte++;
}
void addEdge2(int u, int v, int w)
{
edge2[cnte2].u = u; edge2[cnte2].v = v;
edge2[cnte2].w = w; edge2[cnte2].next = head2[u];
head2[u] = cnte2++; edge2[cnte2].v = u; edge2[cnte2].u = v;
edge2[cnte2].w = 0; edge2[cnte2].next = head2[v];
head2[v] = cnte2++;
} int spfa()
{
queue<int> q;
for(int i = 1; i <= n; i++)
{
vis[i] = 0;
lowdis[i] = INF;
}
vis[sp] = 1; lowdis[sp] = 0;
q.push(sp);
while(!q.empty())
{
int cur = q.front();
q.pop(); vis[cur] = 0;
for(int i = head[cur]; i != -1; i = edge1[i].next)
{
int v = edge1[i].v;
if(lowdis[v] > lowdis[cur]+edge1[i].w)
{
lowdis[v] = lowdis[cur]+edge1[i].w; if(!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
return lowdis[tp] != INF;
} void dfs(int cur)
{
for(int i = tail[cur]; i != -1; i = edge1[i].next2)
{
int u = edge1[i].u;
if(lowdis[u]+edge1[i].w == lowdis[cur])
{
addEdge2(u, cur, 1);
if(!vis[u])
{
vis[u] = 1;
dfs(u);
}
}
}
} bool bfs()
{
memset(vis, 0, sizeof vis);
memset(dep, -1, sizeof dep);
queue<int> q;
q.push(sp);
vis[sp] = 1;
dep[sp] = 1;
while(!q.empty())
{
int cur = q.front();
q.pop(); for(int i = head2[cur]; i != -1; i = edge2[i].next)
{
int v = edge2[i].v;
if(!vis[v] && edge2[i].w > 0)
{
dep[v] = dep[cur]+1;
vis[v] = 1;
q.push(v);
}
}
} return dep[tp] != -1;
} int dfs2(int cur, int flow)
{
if(cur == tp) return flow;
int res = 0;
for(int i = head2[cur]; i != -1 && flow > res; i = edge2[i].next)
{
int v = edge2[i].v;
if(edge2[i].w > 0 && dep[v] == dep[cur]+1)
{
int x = min(edge2[i].w, flow-res);
int f = dfs2(v, x);
edge2[i].w-=f;
edge2[i^1].w+=f;
res += f;
}
} if(!res) dep[cur] = -1;
return res;
} int dinic()
{
int res = 0;
while(bfs())
{
int t;
while(t = dfs2(sp, INF))
{
res += t;
}
}
return res;
} int main()
{
//freopen("C:\\Users\\Admin\\Desktop\\in.txt", "r", stdin);
int t; scanf("%d", &t);
while(t--)
{
cnte = 0;
cnte2 = 0;
memset(head, -1, sizeof head);
memset(head2, -1, sizeof head2);
memset(tail, -1, sizeof tail);
int u, v, w;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
addEdge1(u, v, w);
}
scanf("%d%d", &sp, &tp);
int ans;
if(!spfa()) ans = 0;
else
{
dfs(tp);
ans = dinic();
}
printf("%d\n", ans);
}
return 0;
}
HDU 3416的更多相关文章
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- 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 3081 hdu 3277 hdu 3416 Marriage Match II III IV //灵活运用最大流量
3081 意甲冠军: n女生选择不吵架,他甚至男孩边(他的朋友也算.并为您收集过程).2二分图,一些副作用,有几个追求完美搭配(每场比赛没有重复的每一个点的比赛) 后.每次增广一单位,(一次完美匹配) ...
- hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用
3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配 ...
- HDU 3416 Marriage Match IV dij+dinic
题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...
- O - Marriage Match IV - hdu 3416(最短路+最大流)
题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次. 分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么 ...
- HDU 3416 Marriage Match IV
最短路+最大流 #include<cstdio> #include<cstring> #include<string> #include<cmath> ...
随机推荐
- 石子游戏Kam(bzoj 1115)
Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏 ...
- 大陆争霸(bzoj 1922)
Description 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的 克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭 的神曾·布拉泽,而克里斯国信仰象征光 ...
- 【HDOJ5512】Pagodas(数论)
题意:给定n,a,b,一开始集合里面有两个数:a和b,然后两个人轮流往这个集合里面增加数字 增加数字的原则是:这个集合里面任选两个数的和或差(a + b或a - b或b -a的中的任意一个没被选中的属 ...
- aspx生成静态页面html 例子
原文发布时间为:2009-07-26 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- linux下的程序调试方法汇总
搞电子都知道,电路不是焊接出来的,是调试出来的.程序员也一定认同,程序不是写出来的,是调试出来的.那么调试工具就显得尤为重要,linux作为笔者重要的开发平台,在linux中讨论调试工具主要是为那些入 ...
- hdu 4530(数学)
小Q系列故事——大笨钟 Time Limit: 600/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
- AC日记——封锁阳光大学 洛谷 P1330
封锁阳光大学 思路: bfs染色: 如果当前点能通往已染色的点则不能完成: 图不一定联通: 来,上代码: #include <queue> #include <cstdio> ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom-强连通分量(Tarjan)
本来分好组之后,就确定好了每个人要学什么,我去学数据结构啊. 因为前一段时间遇到一道题是用Lca写的,不会,就去学. 然后发现Lca分为在线算法和离线算法,在线算法有含RMQ的ST算法,前面的博客也写 ...
- 牛客网 牛客小白月赛1 D.多项式乘法
D.多项式乘法 链接:https://www.nowcoder.com/acm/contest/85/D来源:牛客网 这个题想一下就能想出来了. 代码: 1 #include<iostrea ...