转载请注明出处: 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. Light oj 1030 二分查找

    1088 - Points in Segments   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  2. C++11 lambda表达式学习

    lambda表达式是函数式编程的基础.咱对于函数式编程也没有足够的理解,因此这里不敢胡言乱语,有兴趣的可以自己查找相关资料看下.这里只是介绍C++11中的lambda表达式自己的认识.这里有参考文档h ...

  3. WCF+AJAX最佳实践

    本文是基于Frank Xu的一个webcast上的串并总结,图片等都截至视频,谨致谢. 路线图 什么是WCF Windows Communication Foundation是MS为构建面向服务的应用 ...

  4. 获取当前PHP运行环境是否cli模式

    需要用到系统函数php_sapi_name() 或者 系统常量 PHP_SAPI,返回 cli 或 cli_server /* 判断当前的运行环境是否是cli模式 */ function is_cli ...

  5. WDCP LNMPA和LNMP 504 Gateway time-out错误的解决方法

    Nginx的特点是处理静态很给力,Apache的特点是处理动态很稳定,两者结合起来便是LNMPA,nginx处理前端,apache处理后端,这样处理静态会很快,处理动态会很稳定.当我以为安装完成以后便 ...

  6. [HDU] 2094 产生冠军(拓扑排序+map)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2094 注意每组数据处理前,map要清空. #include<cstdio> #includ ...

  7. sizeof与类,继承,virtual的种种

    对虚继承层次的对象的内存布局,在不同编译器实现有所区别. 首先,说说GCC的编译器. 它实现比较简单,不管是否虚继承,GCC都是将虚表指针在整个继承关系中共享的,不共享的是指向虚基类的指针. clas ...

  8. 统计维护<第四篇>

    SQL Server允许用户手工地控制单独数据库中的统计维护.SQL Server的4个主要的控制紫铜统计的维护的配置如下: 在无索引的列上新建统计(自动创建统计): 更新现有统计(自动更新统计): ...

  9. 【转】如何在ubuntu12.04设置adb驱动

    原文网址:http://www.xuebuyuan.com/1475698.html 在ubuntu上adb驱动不用像在windows上一样需要额外装,只需要写一个配置文件就可以,下面是设置的步骤: ...

  10. Hdu4742-Pinball Game 3D(cdq分治+树状数组)

    Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...