hdu3416 判断最短路是否唯一(每条边只能走一次)
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
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.
#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 判断最短路是否唯一(每条边只能走一次)的更多相关文章
- poj 1679 Prim判断次短路
题意:判断最短路是否唯一. 思路:先prrim一次求出最短路同时记录最短路加入的边: 然后枚举所求边,将其删除再求n-1次prim,判断再次所求得的最短路与第一次求得的次短路的关系. 代码: #inc ...
- ZOJ - 2587 Unique Attack (判断最小割是否唯一)
题意:判断最小割是否唯一. 分析:跑出最大流后,在残余网上从源点和汇点分别dfs一次,对访问的点都打上标记. 若还有点没有被访问到,说明最小割不唯一. https://www.cnblogs.com/ ...
- CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数
题意: 给出n个点m条公路k条铁路. 接下来m行 u v w //u->v 距离w 然后k行 v w //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...
- hdu-3416(最短路+网络流)
题意:给你一个有向权图,问你从S到E有几条最短路,每条边直走一次的情况下: 解题思路:每条边直走一次,最大流边权为1,因为要算几条最短路,那么能得到最短路的路径标记下,然后跑最大流 代码: #incl ...
- 判断强联通图中每条边是否只在一个环上(hdu3594)
hdu3594 Cactus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu3416+hdu6582(最短路+最大流)
题意 hdu3416: 给一个图,边不能重复选,问有多少个最短路 hdu6582: 给一个图,问最少删除边权多少的边后,最短路长度增加 分析 边不能重复选这个条件可以想到边权为1,跑最大流,所以我们可 ...
- POJ 1679 判断最小树是否唯一
题意: 给你一个图,问你最小树是否唯一,唯一则输出最小数的权值,不唯一输出Not Unique! 思路: 题目问的是最小树是否唯一,其实也就是在问次小树是否等于最小树,如果等于则 ...
- mysql中判断表中是否存在某条记录
SELECT CASE WHEN EXISTS (SELECT * FROM usergroupmap WHERE groupId = groupIdIn AND userId = v_friendI ...
- objectarx之判断三点是否在一条直线上
bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...
随机推荐
- BZOJ 1304: [CQOI2009]叶子的染色
1304: [CQOI2009]叶子的染色 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 566 Solved: 358[Submit][Statu ...
- win7 远程桌面关机
在任务管理器中, 打开运行窗口, 执行 shutdown -s 命令, 将在30秒后关闭win7, 如果需要更快, 加上 -t 10 参数 关于 shutdown 的命令行说明: C:\Users\R ...
- Struts2动态方法调用(DMI)
当structs.xml解析到Action的时候,默认执行的是此action的execute()方法,但是实际开发中,我们的action中含有很多方法,比如说增删改查的方法,那么structs.xml ...
- SPM - data analysis
来源: SPM基本原理与使用PPT, 北师大,朱朝喆研究员,http://www.cnblogs.com/haore147/p/3633515.html ❤ First-level analysis: ...
- 判断百度某一经纬度的地图颜色值python
from PIL import Image import MySQLdb import os import urllib import time from multiprocessing.dummy ...
- JQuery阻止事件冒泡---阻止后续代码执行
(1)什么是事件起泡 首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什么要传播呢 ...
- 解决jquery.validate.js的验证bug
版本提示:jq为1.4.4, jquery.validate 为jQuery validation plug-in 1.7 问题: a.选填选项,如邮箱设置格式验证,那么情况输入框,验证label变成 ...
- easyui layout 折叠后显示标题
(function($){ var buttonDir = {north:'down',south:'up',east:'left',west:'right'}; $.extend($.fn.layo ...
- 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--瘦身计划
Orchard CMS是针对CMS开发的,对于很多开发需求来说,内容管理这块儿可能并不需要,而需要它的模块式开发模式.所以我这里通过对OrchardCMS进行瘦身,去除 内容管理部分的内容,保留简单的 ...
- Java应用程序项目的打包与发行(run.bat形式)
参考: http://www.iteye.com/topic/57312 背景: 以前一直都是在eclipse上面创建应用程序,每次要要运行的时候都要打开eclipse, 直到有个同事叫我帮忙写一个应 ...