Marriage Match IV

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

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
 
题意:
有n个城市,m条边,a到b耗费为c,为单向边。要求从s到t的最短路径有多少条,每一条边只能走一次。
 
思路:
如果每天边不一定走一次的话,那么可以通过树形dp来求解。但是这里每条边只能走一次,也就是说每条路径上面的边的流量为1,从起点到终点求一次最大流即可。这样题目就能够用最大流来解决。对于建立新的图,可以先从终点到起点求一次最短路,然后从起点开始dfs,并且维护now[]数组,表示从起点到这个点的路径长度,
如果now[rt] + dis[t] + edge[i].val == dis[S](dis[]的起点是原本图中的终点),那么说明这两个点是最短路上的点,那么可以连边,流量为1,。跑一次最大流解决问题了。
 
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int to;
int val;
int next;
}edge[MAXN**],e[MAXN**];
int ind,pre[MAXN],vis[MAXN],dis[MAXN],pre1[MAXN],ind1;
int now[MAXN],S,T;
int n,m;
void add1(int x,int y,int z)
{
e[ind1].to = y;
e[ind1].val = z;
e[ind1].next = pre1[x];
pre1[x] = ind1 ++;
}
void spfa()
{
for(int i = ; i <= n; i++){
dis[i] = INF;
vis[i] = ;
}
vis[T] = ;
dis[T] = ;
queue<int>q;
q.push(T);
while(!q.empty()){
int tp = q.front();
q.pop();
vis[tp] = ;
for(int i = pre1[tp]; i != -; i = e[i].next){
int t = e[i].to;
if(dis[t] > dis[tp] + e[i].val){
dis[t] = dis[tp] + e[i].val;
if(!vis[t]){
vis[t] = ;
q.push(t);
}
}
}
}
}
void add(int x,int y,int z)
{
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs1(int rt)
{
vis[rt] = ;
if(rt == T)return ;
for(int i = pre1[rt]; i != -; i = e[i].next){
int t = e[i].to;
if(now[rt] + dis[t] + e[i].val == dis[S]){
now[t] = now[rt] + e[i].val;
add(rt,t,);
add(t,rt,);
if(!vis[t]){
dfs1(t);
}
}
}
}
int bfs()
{
memset(vis,-,sizeof(vis));
queue<int>q;
vis[S] = ;
q.push(S);
while(!q.empty()){
int tp = q.front();
q.pop();
for(int i = pre[tp]; i != -; i = edge[i].next){
int t = edge[i].to;
if(vis[t] == - && edge[i].val){
vis[t] = vis[tp] + ;
q.push(t);
}
}
}
if(vis[T] == -)return ;
return ;
}
int dfs(int rt,int low)
{
int used = ;
if(rt == T)return low;
for(int i = pre[rt]; i != - && used < low; i = edge[i].next){
int t = edge[i].to;
if(vis[t] == vis[rt] + && edge[i].val){
int a = dfs(t,min(low-used,edge[i].val));
used += a;
edge[i].val -= a;
edge[i^].val += a;
}
}
if(used == )vis[rt] = -;
return used;
}
int x[MAXN*],y[MAXN*],z[MAXN*];
void Init(int flag)
{
ind1 = ;
memset(pre1,-,sizeof(pre1));
for(int i = ; i <= m; i++){
if(!flag){
add1(y[i],x[i],z[i]);
}
else {
add1(x[i],y[i],z[i]);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = ; i <= m; i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
}
Init();
scanf("%d%d",&S,&T);
spfa();
Init();
ind = ;
memset(now,,sizeof(now));
memset(pre,-,sizeof(pre));
dfs1(S);
int ans = ;
while(bfs()){
while(){
int a = dfs(S,INF);
if(!a)break;
ans += a;
}
}
printf("%d\n",ans);
}
return ;
}

hdu3416 判断最短路是否唯一(每条边只能走一次)的更多相关文章

  1. poj 1679 Prim判断次短路

    题意:判断最短路是否唯一. 思路:先prrim一次求出最短路同时记录最短路加入的边: 然后枚举所求边,将其删除再求n-1次prim,判断再次所求得的最短路与第一次求得的次短路的关系. 代码: #inc ...

  2. ZOJ - 2587 Unique Attack (判断最小割是否唯一)

    题意:判断最小割是否唯一. 分析:跑出最大流后,在残余网上从源点和汇点分别dfs一次,对访问的点都打上标记. 若还有点没有被访问到,说明最小割不唯一. https://www.cnblogs.com/ ...

  3. CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数

    题意: 给出n个点m条公路k条铁路. 接下来m行 u v w      //u->v 距离w 然后k行 v w         //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...

  4. hdu-3416(最短路+网络流)

    题意:给你一个有向权图,问你从S到E有几条最短路,每条边直走一次的情况下: 解题思路:每条边直走一次,最大流边权为1,因为要算几条最短路,那么能得到最短路的路径标记下,然后跑最大流 代码: #incl ...

  5. 判断强联通图中每条边是否只在一个环上(hdu3594)

    hdu3594 Cactus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

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

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

  7. POJ 1679 判断最小树是否唯一

    题意:       给你一个图,问你最小树是否唯一,唯一则输出最小数的权值,不唯一输出Not Unique! 思路:      题目问的是最小树是否唯一,其实也就是在问次小树是否等于最小树,如果等于则 ...

  8. mysql中判断表中是否存在某条记录

    SELECT CASE WHEN EXISTS (SELECT * FROM usergroupmap WHERE groupId = groupIdIn AND userId = v_friendI ...

  9. objectarx之判断三点是否在一条直线上

    bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...

随机推荐

  1. VS使用Sublime Text 主题

    VS主题需求 VS默认有三种主题Dark,Light,Blue,但你是否想寻找其它的主题呢?本文介绍两种方法来满足你的不同需求 1.微软官方扩展 微软官方推出的 Visaul Studio Color ...

  2. WEB 业务测试中需要关注的问题

    汇总起来分为:    1.浏览器自身的一些操作,后退键,刷新键,样式兼容,多浏览器之间的一些操作 2.键盘快捷键的一些支持 3.所有前端校验,必须也在后端代码进行校验,验证后端是否校验可越过前端校验进 ...

  3. Guava 是个风火轮之函数式编程(3)——表处理

    云栖社区> 博客列表> 正文 Guava 是个风火轮之函数式编程(3)--表处理 潘家邦 2016-01-26 13:19:21 浏览1062 评论0 java Guava 摘要: 早先学 ...

  4. 如何撰写PRD

    PRD(Product-Requirement-Document,产品需求文档),这对于任何一个产品经理来说都不会陌生的一个文档,一个PRD是衡量一个产品经理整体思维的标准,一个PRD可以看出一个产品 ...

  5. ZooKeeper 笔记(4) 实战应用之【消除单点故障】

    关键节点的单点故障(Single Point of Failure)在大型的架构中,往往是致命的.比如:SOA架构中,服务注册中心(Server Register)统一调度所有服务,如果这个节点挂了, ...

  6. tomcat 新手上路

    前提:本机先安装好JDK,保证常规java环境已经具备 1.下载Tomcat 7.0现在官网上好象已经没有安装程序版了,只有免解压zip版本(现在最新的版本是7.0.42) 下载地址 http://t ...

  7. Java 密码扩展无限制权限策略文件

    因为某些国家的进口管制限制,Java发布的运行环境包中的加解密有一定的限制.比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件.   官方网站提供了JCE无限制权限策略文件的下载:   ...

  8. JQuery 图片略缩与弹出预览 jqthumb fancybox

    弹出框插件-FANCYBOXhttp://www.jq22.com/jquery-info28 jqthumb.js缩略图插件 http://www.ijquery.cn/?p=798

  9. XMl各种格式转换功能代码

    package com.cdv.test; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.File ...

  10. Putty SSH简单使用

    本地的puttygen生出的秘钥,公钥传到服务器上连接会报错 Server refused our key. 一般我们建议都在服务器上生成秘钥,把私钥下载下来.加载到putty认证中 01.在服务器上 ...