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; }

转载于:https://www.cnblogs.com/brucemengbm/p/6809823.html

zoj How Many Shortest Path相关推荐

  1. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  2. P - The Shortest Path in Nya Graph HDU - 4725

    P - The Shortest Path in Nya Graph HDU - 4725 最短路 不是 每两个点之间按层数设置边权 + 额外边权 TLE 是 相邻两层之间设置边权 + 额外边权 需注 ...

  3. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

  4. OSPF(Open Shortest Path First开放式最短路径优先)

    **协议** OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在单一自治系统(a ...

  5. 4kyu Path Finder #2: shortest path

    4kyu Path Finder #2: shortest path 题目背景: Task You are at position [0, 0] in maze NxN and you can onl ...

  6. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  7. AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)

    题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B Single Source Shortest Path ( ...

  8. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  9. CF938G Shortest Path Queries(线性基/线段树分治/异或)

    CF938G Shortest Path Queries 支持加边删边和查询两点之间的异或最短路,我们可以使用线段树分治,然后利用线性基求解. 但是这里图可能不是联通的,所以查询两点之间的异或和需要边 ...

最新文章

  1. SAP 电商云 Spartacus UI 的 urlParameter 配置原理
  2. 如何在Java中检查对象是否为空?
  3. wshttpbinding java_WCF自定义用户账号密码之WCF系结模式wsHttpBinding的Java调用
  4. 中科院动物所团队揭示了空间种子传播集合网络的结构及功能维持机制
  5. 原来编译通过,现在编译不通过,怎么回事?
  6. Linux 安装JDK详细步骤
  7. 卸载mysql服务器_彻底卸载MySQL服务
  8. UNIX网络编程卷1:套接字联网API(第3版).pdf
  9. Windows系统设置共享文件夹及访问共享文件夹
  10. codewars day1
  11. typora偏好设置
  12. KDD2015,Accepted Papers
  13. linux内核纳秒精度时间,在内核获取时间 精度纳秒级
  14. 使用EChat通过php读取后台数据将其转换为饼状图
  15. 【转】Java 多线程学习
  16. 计算机开机需要注意什么,笔记本电脑第一次开机注意事项
  17. nect计算机报名入口,教师资格考试如何报名?
  18. 2021年R2移动式压力容器充装最新解析及R2移动式压力容器充装考试总结
  19. springboot毕设项目教师教学质量评价管理系统td7jn(java+VUE+Mybatis+Maven+Mysql)
  20. 蓝湖、Cutterman使用方法

热门文章

  1. 【机器视觉】机器视觉博客汇总
  2. 【MFC】工具栏按钮单选效果
  3. 【Tools】Linux下C和C++程序中内存泄露检测
  4. 【Linux】一步一步学Linux——mktemp命令(263)
  5. 【Linux网络编程】原始套接字实例:MAC 地址扫描器
  6. 【Linux系统编程】Linux 信号列表
  7. 解析烧录固件失败_化虚为实,示人本相!FLIR热像仪双型号双版本上手解析
  8. php读取excel的公式,PHPExcel在解析xlsx文件中的公式时返回零“0”
  9. Codeforces 987C. Three displays(o(n^2))
  10. 如何在MFC线程中使用控件的成员变量和函数