转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

War

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

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

题意:

问有几种最短路的方案。每条边只能经过一次。

分析:

跑一遍最短路,若某条边是最短路的中的边,则连一条对应的容量为1的边,然后dinic搞一下就行

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 1600
vector<PII> Map[MAXN]; void init() { REP(i,MAXN) Map[i].clear(); } int dis[MAXN];
void dijkstra(int s)
{
REP(i,MAXN){dis[i]=i==s?:INF;}
int vis[MAXN] = {};
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push(MP(,s));
while(!q.empty())
{
PII p = q.top(); q.pop();
int x = p.second;
if(vis[x])continue;
vis[x] = ;
for(vector<PII>::iterator it = Map[x].begin(); it != Map[x].end(); it++)
{
int y = it->first;
int d = it->second;
if(!vis[y] && dis[y] > dis[x] + d)
{
dis[y] = dis[x] + d;
q.push(MP(dis[y],y));
}
}
}
}
struct edge{
int to,cap,rev;
edge(int _to,int _cap,int _rev)
{
to=_to;
cap=_cap;
rev=_rev;
}
};
const int MAX_V=;
vector<edge>G[MAX_V];
int iter[MAX_V];
int level[MAX_V];
int tot=;
void add_edge(int from,int to,int cap)
{
G[from].PB(edge(to,cap,G[to].size()));
G[to].PB(edge(from,,G[from].size()-));
}
void bfs(int s,int t)
{
CLR(level,-);
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<G[u].size();i++)
{
edge &e=G[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int Dinic(int s,int t)
{
int flow=;
for(;;)
{
bfs(s,t);
if(level[t]<)return flow;
memset(iter,,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>)
{
flow+=f;
}
}
} int main()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--){
int n;
init();
scanf("%d",&n);
int u,v,d;
while(scanf("%d%d%d",&u,&v,&d)&&(u||v||d)){
u--;
v--;
Map[u].PB(MP(v,d));
Map[v].PB(MP(u,d));
}
if(n==){
printf("0\n");
continue;
}
for(int i=;i<n;i++)G[i].clear();
dijkstra();
for(int i=;i<n;i++){
for(vector<PII>::iterator it = Map[i].begin(); it != Map[i].end(); it++)
{
int y = it->first;
int d = it->second;
if(dis[i]+d==dis[y]){
add_edge(i,y,);
}
}
}
printf("%d\n",Dinic(,n-));
} return ;
}

代码君

hdu3599 War(最大流)的更多相关文章

  1. ZOJ3508 The War 贪心,最大流

    传送门:I Am Here 常规解法是贪心,但是在复习最大流的写法,因此用sap来写的.思路是很好想的 #include<cstdio> #include<cstdlib> # ...

  2. War(最短路+最大流)

    War http://acm.hdu.edu.cn/showproblem.php?pid=3599 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. [翻译]Kafka Streams简介: 让流处理变得更简单

    Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...

  4. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  5. POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

    http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. HDU4309-Seikimatsu Occult Tonneru(最大流)

    Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. springboot打成war包找不到文件

    项目在eclipse运行是可以的,但是打成war包到线上报错: FileNotFoundException: class path resource [static/apiclient_cert.p1 ...

  8. Kafka Streams简介: 让流处理变得更简单

    Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...

  9. [Java] Windows/Linux路径不同时,统一war的最简办法

    作者: zyl910 一.缘由 在项目开发时,因为运行环境的不同,导致有时得分别为不同的环境,切换配置参数打不同war包.但手工切换配置文件的话,不仅费时费力,而且容易出错. 有些打包工具支持配置切换 ...

随机推荐

  1. 基尔霍夫矩阵题目泛做(AD第二轮)

    题目1: SPOJ 2832 题目大意: 求一个矩阵行列式模一个数P后的值.p不一定是质数. 算法讨论: 因为有除法而且p不一定是质数,不一定有逆元,所以我们用辗转相除法. #include < ...

  2. React/React Native的 ES5 ES6 写法对照

      ES5 ES6 模块 var React = require("react-native); var { Image, Text, View } = React;   import Re ...

  3. python学习第一天 -安装配置及其输入输出

    Python, 是一种面向对象.解释型计算机程序设计语言. python适合领域: 1.Web网络和各种网络服务 2.系统工具和脚本 3.作为“胶水”语言把其他语言开发的模块包装起来方便使用 pyth ...

  4. 答:我们公司的ASP.NET 笔试题,你觉得难度如何

    闲来无事,逛逛园子,发现有个面试题,觉得有意思.已自己的理解答来看看,不足之处,请多指教. 原文地址:http://www.cnblogs.com/leotsai/p/aspnet-tests-for ...

  5. HTML5 例子学习 HT 图形组件

    HTML5 例子学习 HT 图形组件 HT 是啥:Everything you need to create cutting-edge 2D and 3D visualization. 这口号是当年心 ...

  6. BZOJ 1063 道路设计NOI2008

    http://www.lydsy.com/JudgeOnline/problem.php?id=1063 题意:给你一棵树,也有可能是不连通的,把树分成几个链,求每个点到根经过的最大链数最小,而且要输 ...

  7. 做10年Windows程序员与做10年Linux程序员的区别(附无数评论)(开源软件相当于熟读唐诗三百首,不会作诗也会吟)

    如果一个程序员从来没有在linux,unix下开发过程序,一直在windows下面开发程序, 同样是工作10年, 大部分情况下与在linux,unix下面开发10年的程序员水平会差别很大.我写这篇文章 ...

  8. 【转帖】.Net中C#的DllImport的用法

    在 C# 中通过 P/Invoke 调用Win32 DLL http://msdn.microsoft.com/zh-cn/library/aa686045.aspx   大家在实际工作学习C#的时候 ...

  9. VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结

    转帖:http://blog.csdn.net/whygosofar/article/details/2821875 MSDN中对于在不同的配置下Link的LIB作了说明: C Runtime Lib ...

  10. 关于Microsoft app下同义词的整理

    Windows os 以下词表达的是同一个概念 windows store app windows metro app windows modern app windows runtime app w ...