War

http://acm.hdu.edu.cn/showproblem.php?pid=3599

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1870    Accepted Submission(s): 442

Problem Description
In the war between Anihc and Bandzi, Anihc has won Bangzi. At that time, Lvbin, the king of Anihc, want to start from Bangzi to beat Nebir across the channel between them. He let his army get to there as soon as possible, and there located many islands which can be used to have a break. In order to save time, the king forbid the army getting through the same pass for more than one time, but they can reach the same island for as many as times they want.
Yunfeng, the General of the army, must tell how many optimal ship routes are there to the king as soon as possible, or he will be killed. Now he asks for your help. You must help Yunfeng to save his life.
He tells you that there are N island. The islands are numbered from 1 to N(1 is Bangzi and N is Nebir, others are many islands). And there are many ways, each way contain the islands number U and V and the length W. Please output your answer.
 
Input
The first line in the input file contains a single integer number T means the case number. 
Each case contains a number N (N<=1500) means the number of the islands. 
And then many lines follow. Each line contains three numbers: U V W (W<10000), means that the distance between island U and V is W. The input of ways are terminated by “ 0 0 0 ”.
 
Output
Print the number of the optimal ship routes are there after each case in a line.
 
Sample Input
1
6
1 2 1
3 2 1
3 4 1
1 3 2
4 2 2
4 5 1
5 6 1
4 6 2
0 0 0
 
Sample Output
2
 
Author
alpc92
 
Source
 
 
 
 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define INF 0x3f3f3f3f
#define maxn 2005
#define MAXN 1000005
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std; int n;
struct sair{
int len,pos;
bool operator<(const sair &b)const{
return len>b.len;
}
};
int dis[maxn];
int vis[maxn];
vector<pair<int,int> >ve[maxn];
void Dijstra(){
priority_queue<sair>q;
sair tmp;
tmp.len=,tmp.pos=;
dis[]=;
q.push(tmp);
while(!q.empty()){
tmp=q.top();
q.pop();
int pos=tmp.pos;
if(vis[pos]){
continue;
}
vis[pos]=;
for(int i=;i<ve[pos].size();i++){
int f=ve[pos][i].first;
int len=ve[pos][i].second; if(dis[f]>dis[pos]+len){
dis[f]=dis[pos]+len;
tmp.pos=f;
tmp.len=dis[f];
q.push(tmp);
}
}
}
} struct Edge{
int v,next;
int cap,flow;
}edge[MAXN*];//注意这里要开的够大。。不然WA在这里真的想骂人。。问题是还不报RE。。
int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
int cnt=;//实际存储总边数
void isap_init()
{
cnt=;
memset(pre,-,sizeof(pre));
}
void isap_add(int u,int v,int w)//加边
{
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void add(int u,int v,int w){
isap_add(u,v,w);
isap_add(v,u,);
}
bool bfs(int s,int t)//其实这个bfs可以融合到下面的迭代里,但是好像是时间要长
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
dep[t]=;
queue<int>q;
while(!q.empty()) q.pop();
q.push(t);//从汇点开始反向建层次图
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=pre[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(dep[v]==-&&edge[i^].cap>edge[i^].flow)//注意是从汇点反向bfs,但应该判断正向弧的余量
{
dep[v]=dep[u]+;
gap[dep[v]]++;
q.push(v);
//if(v==sp)//感觉这两句优化加了一般没错,但是有的题可能会错,所以还是注释出来,到时候视情况而定
//break;
}
}
}
return dep[s]!=-;
}
int isap(int s,int t)
{
if(!bfs(s,t))
return ;
memcpy(cur,pre,sizeof(pre));
//for(int i=1;i<=n;i++)
//cout<<"cur "<<cur[i]<<endl;
int u=s;
path[u]=-;
int ans=;
while(dep[s]<n)//迭代寻找增广路,n为节点数
{
if(u==t)
{
int f=INF;
for(int i=path[u];i!=-;i=path[edge[i^].v])//修改找到的增广路
f=min(f,edge[i].cap-edge[i].flow);
for(int i=path[u];i!=-;i=path[edge[i^].v])
{
edge[i].flow+=f;
edge[i^].flow-=f;
}
ans+=f;
u=s;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
{
cur[u]=path[v]=i;//当前弧优化
flag=true;
break;
}
}
if(flag)
{
u=v;
continue;
}
int x=n;
if(!(--gap[dep[u]]))return ans;//gap优化
for(int i=pre[u];i!=-;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
{
x=dep[edge[i].v];
cur[u]=i;//常数优化
}
}
dep[u]=x+;
gap[dep[u]]++;
if(u!=s)//当前点没有增广路则后退一个点
u=edge[path[u]^].v;
}
return ans;
}
struct Bian{
int x,y,v;
}B[MAXN]; int main(){ std::ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--){
cin>>n;
for(int i=;i<=n;i++){
dis[i]=INF;
vis[i]=;
}
for(int i=;i<=n;i++){
ve[i].clear();
}
int a,b,c;
int j;
int co=;
while(cin>>B[co].x>>B[co].y>>B[co].v){
if(!B[co].x&&!B[co].y&&!B[co].v){
break;
}
ve[B[co].x].push_back(make_pair(B[co].y,B[co].v));
ve[B[co].y].push_back(make_pair(B[co].x,B[co].v));
co++;
}
Dijstra();
isap_init();
if(dis[n]==INF||n==){
cout<<<<endl;
continue;
}
for(int i=;i<co;i++){///判断是不是最短路上的边
if(dis[B[i].x]-dis[B[i].y]==B[i].v){
add(B[i].y,B[i].x,);
}
else if(dis[B[i].y]-dis[B[i].x]==B[i].v){
add(B[i].x,B[i].y,);
}
}
int s=,t=n;
int ans=isap(s,t);
cout<<ans<<endl;
} }

War(最短路+最大流)的更多相关文章

  1. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  2. BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )

    最短路 + 最大流 , 没什么好说的... 因为long long WA 了两次.... ------------------------------------------------------- ...

  3. 【最短路+最大流】上学路线@安徽OI2006

    目录 [最短路+最大流]上学路线@安徽OI2006 PROBLEM SOLUTION CODE [最短路+最大流]上学路线@安徽OI2006 PROBLEM 洛谷P4300 SOLUTION 先在原图 ...

  4. P3376 【模板】网络最大流——————Q - Marriage Match IV(最短路&最大流)

    第一道题是模板题,下面主要是两种模板,但都用的是Dinic算法(第二个题也是) 第一题: 题意就不需要讲了,直接上代码: vector代码: 1 //invalid types 'int[int]' ...

  5. 【BZOJ-3931】网络吞吐量 最短路 + 最大流

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1228  Solved: 524[Submit][Stat ...

  6. hdu3416 最短路+最大流

    题意:有 n 点 m 边,有出发点 A 到达点 B ,只允许走原图中的最短路,但每条边只允许被走一次,问最多能找出多少条边不重复的最短路 一开始做到的时候瞎做了一发最短路,WA了之后也知道显然不对,就 ...

  7. HDU 5294 Tricks Device 最短路+最大流

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...

  8. SGU 185 Two shortest 最短路+最大流

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...

  9. O - Marriage Match IV - hdu 3416(最短路+最大流)

    题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次.   分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么 ...

随机推荐

  1. RouterOS 设定NAT loopback (Hairpin NAT)回流

    In the below network topology a web server behind a router is on private IP address space, and the r ...

  2. PreApplicationStartMethodAttribute的使用

    先预备一个类,用于Start时调用 public static class MyPreApplicationStart { public static void RegisterGlobalFilte ...

  3. python 四种数值类型(int,long,float,complex)介绍

    Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下. 数字数据 ...

  4. NFS各个版本之间的比较

    NFS是一种网络文件系统,从1985年推出至今,共发布了3个版本:NFSv2.NFSv3.NFSv4,NFSv4包含两个次版本NFSv4.0和NFSv4.1.经过20多年发展,NFS发生了非常大的变化 ...

  5. 一起KVM环境下windows7虚拟机异常死机(BSOD)的问题解决

    先说一下环境: 一.硬件 8台服务器做的超融合架构,软件存储池, 每台服务器是96G内存,两颗Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz,32线程. 每台服务器是 ...

  6. redis入门之jedis

    jedis是redis官方首选的java客户端开发包 开源托管地址:https://github.com/xetorthio/jedis 下载地址,以及maven, 依赖参考: 下面来编写一段程序进行 ...

  7. selenium+python自动化84-chrome手机wap模式(登录淘宝页面)

    前言 chrome手机wap模式登录淘宝页面,点击验证码无效问题解决. 切换到wap模式,使用TouchActions模块用tap方法触摸 我的环境 chrome 62 chromedriver 2. ...

  8. UVA-550

    题意 输入进制数n,第一个乘数的最后一位m,第二个乘数k,乘法的结果为mk, mk的第一位是m,求此时mk的长度 #include<iostream> #include <stdio ...

  9. Struts2:No result defined for action com.yibai.user.action.LoginAction and result input

    转自:https://zhidao.baidu.com/question/133574016.html 1 String 里面有5个static 常量分别是: ERROR INPUT LOGIN NO ...

  10. VB 调用动态链接库

    作为一种简单易用的Windows开发环境,Visual Basic从一推出就受到了广大编程人员的欢迎.它使 程序员不必再直接面对纷繁复杂的Windows消息,而可以将精力主要集中在程序功能的实现上,大 ...