zoj How Many Shortest Path
How Many Shortest Path
题目:
给出一张图,求解最短路有几条。处理特别BT。还有就是要特别处理map[i][i] = 0,数据有不等于0的情况!
竟然脑残到了些错floyd!
!。!!
!
14次wrong。!
!。。!
。。
算法:
先最短路处理,把在最短路上的边加入上。既是。dist[s][i] + map[i][j] == dist[s][j]表示从起点到i点加上当前边是最短路。把这个加入到网络流边集中。容量为1.然后,建立一个超级源点。容量为INF。
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std; const int INF = 1 << 30;
const int MAXN = 200 + 10;
struct Edge{
int from,to,cap,flow;
Edge(){};
Edge(int _from,int _to,int _cap,int _flow)
:from(_from),to(_to),cap(_cap),flow(_flow){};
};
vector<Edge> edges;
vector<int> G[MAXN];
int cur[MAXN],d[MAXN];
bool vst[MAXN];
int N,M,src,sink; int mat[MAXN][MAXN],dist[MAXN][MAXN];
int ss,tt; void init(){
for(int i = 0;i <= N+5;++i)
G[i].clear();
edges.clear();
} void addEdge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
int sz = edges.size();
G[from].push_back(sz - 2);
G[to].push_back(sz - 1);
} void flody(){
memcpy(dist,mat,sizeof(mat)); for(int i = 0;i < N;++i)
for(int j = 0;j < N;++j)
dist[i][j] = (dist[i][j]==-1? INF:dist[i][j]); for(int k = 0;k < N;++k)
for(int i = 0;i < N;++i)
for(int j = 0;j < N;++j){
// if(dist[i][k] == INF||dist[k][j] == INF) continue;
if(dist[i][k] < INF && dist[k][j] < INF&&dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
} bool BFS(){
memset(vst,0,sizeof(vst));
queue<int> Q;
Q.push(src);
d[src] = 0;
vst[src] = 1; while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int i = 0;i < (int)G[u].size();++i){
Edge& e = edges[G[u][i]];
if(!vst[e.to] && e.cap > e.flow){
vst[e.to] = 1;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
} return vst[sink];
} int DFS(int x,int a){
if(x == sink || a == 0)
return a; int flow = 0,f;
for(int& i = cur[x];i < (int)G[x].size();++i){
Edge& e = edges[G[x][i]];
if(d[e.to] == d[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0){
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
} return flow;
} int maxFlow(){
int flow = 0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow += DFS(src,INF);
}
return flow;
} int main()
{
// freopen("Input.txt","r",stdin); while(~scanf("%d",&N)){
init();
for(int i = 0;i < N;++i)
for(int j = 0;j < N;++j){
scanf("%d",&mat[i][j]);
} for(int i = 0;i < N;++i)
mat[i][i] = 0; scanf("%d%d",&ss,&tt);
flody(); if(ss == tt){
printf("inf\n");
continue;
} src = N + 2; sink = tt;
addEdge(src,ss,INF);
// 建图
for(int i = 0;i < N;++i){
if(dist[ss][i] == INF) continue;
for(int j = 0;j < N;++j){
if(i == j) continue;
if(dist[ss][j] == INF||mat[i][j] == -1) continue;
if(dist[ss][i] + mat[i][j] == dist[ss][j]){ //该边是否在最短路上
addEdge(i,j,1);
}
}
}
printf("%d\n",maxFlow());
}
return 0;
}
zoj How Many Shortest Path的更多相关文章
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]
人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...
- ZOJ 2760 How Many Shortest Path(最短路径+最大流)
Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
随机推荐
- Python 列表相关
python列表 列表推导式 例1 [ i*i for i in range(10) ] 打印如下: >>> [i*i for i in range(10)] [0, 1, 4, 9 ...
- GYM 101350 G
G. Snake Rana time limit per test 4.0 s memory limit per test 256 MB input standard input output sta ...
- C# 导出Excel的示例
Excel知识点. 一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual S ...
- 基于注解实现SpringBoot多数据源配置
1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...
- PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)
L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...
- 谈Elasticsearch下分布式存储的数据分布
对于一个分布式存储系统来说,数据是分散存储在多个节点上的.如何让数据均衡的分布在不同节点上,来保证其高可用性?所谓均衡,是指系统中每个节点的负载是均匀的,并且在发现有不均匀的情况或者有节点增加/删除 ...
- BZOJ 1879 [Sdoi2009]Bill的挑战 ——状压DP
本来打算好好写写SDOI的DP题目,但是忒难了, 太难了,就写的这三道题仿佛是可做的. 生在弱省真是兴奋. 这题目直接状压,f[i][j]表示匹配到i,状态集合为j的方案数,然后递推即可. #incl ...
- Bzoj1083 1083: [SCOI2005]繁忙的都市【MST】
大水题,真不知道出题者是怎么把这么水的题出的这么长的TAT 其实这题在于考语文水平,一共三个要求,前两个要求意思就是要选出的道路是树形的,最后一个要求就是要权值最小,于是整个题意说白了就是求一棵MST ...
- net8:XML的读写操作【广告控件的XML文件实例】
原文发布时间为:2008-08-05 -- 来源于本人的百度文章 [由搬家工具导入] 【用了datalist控件,datalist控件自己学会,主要知道其他按钮COMMANDNAME属性应该改为edi ...
- Linux下重启就需要重新激活eth0的解决办法(ifup eth0)
新安装linux系统,网卡不能自动激活去获取ip,每次都需要手工执行以下命令 ifup eth0 后续通过将ONBOOT=yes这句就能开机启动自动激活,就可以解决问题 vim /etc/syscon ...