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. zufeoj Electrification Plan (最小生成树,巧妙设e[i][j]=0)

    Electrification Plan 时间限制: 1 Sec  内存限制: 128 MB提交: 31  解决: 13[提交][状态][讨论版] 题目描述 Some country has n ci ...

  2. Java-Runoob-高级教程-实例-数组:08. Java 实例 – 数组填充

    ylbtech-Java-Runoob-高级教程-实例-数组:08. Java 实例 – 数组填充 1.返回顶部 1. Java 实例 - 数组填充  Java 实例 以下实例我们通过 Java Ut ...

  3. 管理oracle的一些知识

    管理一个oralce软件: 如何管理数据库,须知道什么知识. 1.安装:位置,字符集 2.建库:什么数据库名称 3.数据库启动: nomout:读参数文件,一些初始化设置信息 mount:读取控制文件 ...

  4. python logging模块使用教程

    简单使用 #!/usr/local/bin/python # -*- coding:utf-8 -*- import logging logging.debug('debug message') lo ...

  5. solr6.3根据搜索关键词词频(关键词出现次数、关键词highlight)进行排序

    http://localhost:8080/solr/test/select?fq=product_name:大有&indent=on&q=product_name:大有电钻 OR r ...

  6. Java内存原型分析:基本知识

    转载: Java内存原型分析:基本知识 java虚拟机内存原型 寄存器:我们在程序中无法控制 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 堆:存放用new产生的数据 静 ...

  7. 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5 20165233

    Exp3 免杀原理与实践 实验内容 一.基础问题回答 1.杀软是如何检测出恶意代码的? 基于特征码的检测:通过与自己软件中病毒的特征库比对来检测的. 启发式的软件检测:就是根据些片面特征去推断.通常是 ...

  8. tornado-请求与响应

    import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供 ...

  9. UVA-568-数论

    题意 输入一个n,求n!最后一个不是0的数 2x5肯定是等于10的,先把所有不是2和5的数乘起来,保留最后一位 计算过程中计算出2和5的个数 因为2*5=10,而且2的个数比5的个数多,所以最后只要把 ...

  10. git超详细教程留着当手册

    GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化Git仓库 :git init ; -- 提交改变到缓存 :gi ...