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的更多相关文章

  1. zoj 2760 How Many Shortest Path 最大流

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

  2. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  3. ZOJ 2760 How Many Shortest Path(最短路径+最大流)

    Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...

  4. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  6. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  7. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  8. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. (中等) 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 ...

随机推荐

  1. Web框架之Django_06 模型层了解(F查询、Q查询、事务、update和save、only和defer、choice属性、bulk_create)

    摘要: F查询 Q查询 事务 一.F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢?Django 提供 F() 来 ...

  2. 数据结构( Pyhon 语言描述 ) — — 第5章:接口、实现和多态

    接口 接口是软件资源用户可用的一组操作 接口中的内容是函数头和方法头,以及它们的文档 设计良好的软件系统会将接口与其实现分隔开来 多态 多态是在两个或多个类的实现中使用相同的运算符号.函数名或方法.多 ...

  3. JS(原型和原型链)

    (学习自慕课网<前端JavaScript 面试技巧> JS(原型和原型链) 题目1.如何准确判断一个变量是数组类型 使用 instanceof 方法 题目2.写一个原型链继承的例子 实例: ...

  4. vue 判断属性是否为object

    //递归对象 var recursiveObject = Vue.extend({ name: 'recursive-object', template:[ '<ul>', '<li ...

  5. Objective-c 实例变量的访问级别

    在C#和JAVA中无论是method还是variable都有严格的访问级别控制,那么在object-c中对访问级别的使用非常稀少,原因可能是因为在method上没有访问级别的语法,单单控制变量没有什么 ...

  6. 关于面试总结-SQL经典面试题

    关于面试总结6-SQL经典面试题 前言 用一条SQL 语句查询xuesheng表每门课都大于80 分的学生姓名,这个是面试考sql的一个非常经典的面试题 having和not in 查询 xueshe ...

  7. pycharm中脚本执行的3种模式(unittest框架、pytest框架、普通模式)

    背景知识,某次使用HTMLTestRunner的时候,发现一直都无法导出报告,后来查询资料发现了一些坑,现在整理一下来龙去脉. 一:pycharm默认的是pytest框架去执行unittest框架的测 ...

  8. 图论trainning-part-1 A. 最短路

    A. 最短路 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name ...

  9. PHP中file_put_contents追加和换行的实现方法

    在PHP的一些应用中需要写日志或者记录一些信息,这样的话.可以使用fopen(),fwrite()以及 fclose()这些进行操作.也可以简单的使用file_get_contents()和file_ ...

  10. iOS知识列表

    Xib和StoryBoard,自动布局地图导航,实时公交,第三方地图应用本地推送通知,网络推送通知,真机调试,应用上传绘图,图表,曲线图 Xcode使用技巧多线程,runtime机制编码解码,加密设备 ...