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. js中,object可以调用style对象,[]不可以调用style对象

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" Content=&q ...

  2. PHP 弹出文件下载 原理 代码

    /** * @author      default7<default7@zbphp.com> * @description 演示PHP弹出下载的原理 * * @param $file_n ...

  3. CA双向认证的时候,如果一开始下载的证书就有问题的,怎么保证以后的交易没有问题?

    研究HTTPS协议的时候,发现网站的CA认证,比如建行,比如支付宝,需要首先下载数字证书, 当然有些其他的双向认证,比如之前做过的港航和JP MORGAN进行交互的时候,证书是私下发送的,不需要去公网 ...

  4. Protocol Buffer Basics: Python

    原文https://developers.google.com/protocol-buffers/docs/pythontutorial Protocol Buffer Basics: Python ...

  5. 【C++11新特性】 - 空间配置allocator类

    原文链接: http://blog.csdn.net/Xiejingfa/article/details/50955295 今天我们来讲讲C++的allocator类. C++提供了new和delet ...

  6. Dividing Infinity - Distributed Partitioning Schemes

    This is the second post in a series discussing the architecture and implementation of massively para ...

  7. 小朋友学C++(1)

    Hello World! 在学C++之前,最好先学习一下C语言 让我们先运行一段简单的代码,编译器可以使用 在线C++编译器 或 Xcode(苹果系统) 或Dev C++(Windows系统). #i ...

  8. SSH 在ssh-copy-id 之后仍需输入密码的问题

    最近在使用Ansible,基于SSH. 远程服务器IP: 192.168.200.193 以下提及的远程服务器都为该服务器. 远程用户: ansible_user 在本地服务器中,ssh-keygen ...

  9. WGCNA 分析

    https://www.jianshu.com/p/f80de3468c04 https://mp.weixin.qq.com/s/-DthUKY2RTY6vxtxapzLkw https://www ...

  10. 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)

    转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...