hdu 3416 Marriage Match IV

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

题目大意:有n个城市m条路,从城市A到城市B的最短路径有几条。

解题思路:先正向反向求最短路,获得起点到每点的最短距离d1[]。 终点到每点的最短距离d2[],最短路Min。然后遍历每一条边。当d1[edges.from]+edges.dis+d2[edges.to]==Min时。将该边增加最大流的图中,容量为1,建完图后,以A为源点,B为汇点跑最大流就可以。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue> using namespace std; const int INF = 0x3f3f3f3f;
const int N = 2005;
const int M = 200005;
typedef long long ll;
int n, m, s, t, Min; struct Edge{
int from,to;
ll dist;
};
struct HeapNode{
int d,u;
bool operator < (const HeapNode& rhs) const{
return d > rhs.d;
}
}; struct Dinic{
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M]; void init() {
ec = 0;
memset(first, -1, sizeof(first));
} void addEdge(int a,int b,int c) {
to[ec] = b;
v[ec] = c;
Next[ec] = first[a];
first[a] = ec++; to[ec] = a;
v[ec] = 0;
Next[ec] = first[b];
first[b] = ec++;
} int BFS() {
int kid, now, f = 0, r = 1, i;
memset(lev, 0, sizeof(lev));
que[0] = s, lev[s] = 1;
while (f < r) {
now = que[f++];
for (i = first[now]; i != -1; i = Next[i]) {
kid = to[i];
if (!lev[kid] && v[i]) {
lev[kid] = lev[now] + 1;
if (kid == t) return 1;
que[r++] = kid;
}
}
}
return 0;
} int DFS(int now, int sum) {
int kid, flow, rt = 0;
if (now == t) return sum;
for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
head[now] = i;
kid = to[i];
if (lev[kid] == lev[now] + 1 && v[i]) {
flow = DFS(kid, min(sum - rt, v[i]));
if (flow) {
v[i] -= flow;
v[i^1] += flow;
rt += flow;
} else lev[kid] = -1;
}
}
return rt;
} int dinic() {
int ans = 0;
while (BFS()) {
for (int i = 0; i <= n; i++) {
head[i] = first[i];
}
ans += DFS(s, INF);
}
return ans;
}
}din; struct Dijkstra{
int n,m; //点数和边数
vector<Edge> edges; //边列表
vector<int> G[M]; //每一个结点出发的边编号(从0開始编号)
bool done[N]; //是否已永久标号
int d[N]; //s到各个点的距离
ll L; void init(int n) {
this->n = n;
for(int i = 0; i <= m * 2; i++) G[i].clear();//清空邻接表
edges.clear();//清空边表
} void addEdge(int from, int to, ll dist) {
//假设是无向图。每条无向边需调用两次AddEdge
edges.push_back((Edge){from, to, dist});
m = edges.size();
G[from].push_back(m - 1);
} void dijkstra(int s) {//求s到全部点的距离
priority_queue<HeapNode> Q;
for(int i = 0; i <= n; i++) d[i] = INF;
d[s] = 0;
memset(done, 0, sizeof(done));
Q.push((HeapNode){0, s});
while(!Q.empty()){
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].size(); i++){
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist){
d[e.to] = d[u] + e.dist;
Q.push((HeapNode){d[e.to], e.to});
}
}
}
} }dij, dij2; void input() {
int u, v, d;
scanf("%d %d", &n, &m);
dij.init(n);
dij2.init(n);
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &u, &v, &d);
dij.addEdge(u, v, d);
dij2.addEdge(v, u, d);
}
scanf("%d %d", &s, &t);
dij.dijkstra(s);
dij2.dijkstra(t);
Min = dij.d[t];
} void solve() {
din.init();
for (int i = 0; i < m; i++) {
if (dij.d[dij.edges[i].from] + dij2.d[dij.edges[i].to] + dij.edges[i].dist == Min) {
din.addEdge(dij.edges[i].from, dij.edges[i].to, 1);
}
}
printf("%d\n", din.dinic());
} int main() {
int T;
scanf("%d", &T);
while (T--) {
input();
solve();
}
return 0;
}

hdu 3416 Marriage Match IV (最短路+最大流)的更多相关文章

  1. HDU 3416 Marriage Match IV (最短路建图+最大流)

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

  2. HDU 3416 Marriage Match IV (Dijkstra+最大流)

    题意:N个点M条边的有向图,给定起点S和终点T,求每条边都不重复的S-->T的最短路有多少条. 分析:首先第一步需要找出所有可能最短路上的边.怎么高效地求出呢?可以这样:先对起点S,跑出最短路: ...

  3. HDU 3416 Marriage Match IV (最短路径&&最大流)

    /*题意: 有 n 个城市,知道了起点和终点,有 m 条有向边,问从起点到终点的最短路一共有多少条.这是一个有向图,建边的时候要注意!!解题思路:这题的关键就是找到哪些边可以构成最短路,其实之前做最短 ...

  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

    最短路+最大流 #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

  7. HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】

    <题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...

  8. HDU 3416 Marriage Match IV(ISAP+最短路)题解

    题意:从A走到B,有最短路,问这样不重复的最短路有几条 思路:先来讲选有效边,我们从start和end各跑一次最短路,得到dis1和dis2数组,如果dis1[u] + dis2[v] + cost[ ...

  9. HDU 3416 Marriage Match IV(最短路,网络流)

    题面 Do not sincere non-interference. Like that show, now starvae also take part in a show, but it tak ...

随机推荐

  1. IOCP模型总结(总结回想)

    IOCP旧代码重提.近期一直在玩其它方面的东东.时不时回想一下,收益多多. IOCP(I/O Completion Port,I/O完毕port)是性能最好的一种I/O模型.它是应用程序使用线程池处理 ...

  2. HDU1796 How many integers can you find【容斥定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包 ...

  3. 【Uva 1633】Dyslexic Gollum

    [Link]: [Description] 输入正整数n和k(1≤n≤400,1≤k≤10),求长度为n的01串中有多少个不含长度至少 为k的回文连续子串.例如,n=k=3时只有4个串满足条件:001 ...

  4. [Angular & Unit Testing] Testing Component with Store

    When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...

  5. ELK之日志查询、收集与分析系统

    项目由来 (1)开发人员不能登录线上服务器查看详细日志,经过运维周转费时费力 (2)日志数据分散在多个系统,难以查找与整合 (3)日志数据量巨大,查询速度太慢,无法满足需求 (4)无法全局掌控项目运行 ...

  6. JavaScript--数据结构之栈

    4.1栈是一种高效的数据结构,是一种特殊的列表.栈内元素只能通过列表的一端访问,也就称为栈顶.后入的先出的操作.Last in First out.所以他的访问每次是访问到栈顶的元素,要想访问其余的元 ...

  7. Angular:了解Typescript

    Angular是用Typescript构建的.因此在学习Angular之前有必要了解一下Typescript. [ 类型 ] Typescript增加了类型系统,好处是: 1. 有助于代码编写,预防在 ...

  8. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~

  9. BZOJ4196: [Noi2015]软件包管理器(树链剖分)

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  10. 【Educational Codeforces Round 35 A】 Nearest Minimums

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找出最小的数字的位置. 最近的肯定是相邻的某对. [代码] #include <bits/stdc++.h> using ...