ZOJ_1760

只要保留S-T所有可能的最短路上的边,然后做最大流即可,题目数据存在f[i][i]!=0的情况,因此如果用floyd预处理的话要注意初始化f[i][i]=0。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 110
#define MAXM 20010
#define INF 0x3f3f3f3f
int N, f[MAXD][MAXD], g[MAXD][MAXD], first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM];
int d[MAXD], q[MAXD], work[MAXD], S, T;
void init()
{int i, j, k;for(i = 1; i <= N; i ++)for(j = 1; j <= N; j ++)scanf("%d", &g[i][j]), f[i][j] = g[i][j] == -1 ? INF : g[i][j];for(i = 1; i <= N; i ++)f[i][i] = 0;for(k = 1; k <= N; k ++)for(i = 1; i <= N; i ++)for(j = 1; j <= N; j ++)f[i][j] = std::min(f[i][j], f[i][k] + f[k][j]);
}
void add(int x, int y, int z)
{flow[e] = z, v[e] = y;next[e] = first[x], first[x] = e ++;
}
int bfs()
{int i, j, rear = 0;memset(d, -1, sizeof(d));d[S] = 0, q[rear ++] = S;for(i = 0; i < rear; i ++)for(j = first[q[i]]; j != -1; j = next[j])if(flow[j] && d[v[j]] == -1){d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];if(v[j] == T)return 1;}return 0;
}
int dfs(int cur, int a)
{if(cur == T)return a;int t;for(int &i = work[cur]; i != -1; i = next[i])if(flow[i] && d[v[i]] == d[cur] + 1)if(t = dfs(v[i], a < flow[i] ? a : flow[i])){flow[i] -= t, flow[i ^ 1] += t;return t;}return 0;
}
int dinic()
{int ans = 0, t;while(bfs()){memcpy(work, first, sizeof(first));while(t = dfs(S, INF))ans += t;}return ans;
}
void solve()
{int i, j, k;scanf("%d%d", &S, &T);++ S, ++ T;if(S == T){printf("inf\n");return ;}if(f[S][T] == INF){printf("0\n");return ;}memset(first, -1, sizeof(first));e = 0;for(i = 1; i <= N; i ++)for(j = 1; j <= N; j ++)if(i != j && g[i][j] != -1){if(f[S][i] + g[i][j] + f[j][T] == f[S][T])add(i, j, 1), add(j, i, 0);}printf("%d\n", dinic());
}
int main()
{while(scanf("%d", &N) == 1){init();solve();}return 0;
}

转载于:https://www.cnblogs.com/staginner/archive/2012/08/06/2625922.html

ZOJ 1760 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 (不相交的最短路径个数)

    [题意]给定一个N(N<=100)个节点的有向图,求不相交的最短路径个数(两条路径没有公共边). [思路]先用Floyd求出最短路,把最短路上的边加到网络流中,这样就保证了从s->t的一个 ...

  3. ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760 题意: 给出一张有向带边权图,若不联通为-1.问不重叠的最 ...

  4. zoj How Many Shortest Path

    How Many Shortest Path 题目: 给出一张图,求解最短路有几条.处理特别BT.还有就是要特别处理map[i][i] = 0,数据有不等于0的情况! 竟然脑残到了些错floyd! ! ...

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

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

  6. [CF843D]Dynamic Shortest Path

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

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

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

  8. 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 ...

  9. 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 ...

最新文章

  1. redmine忘记username和password
  2. 树莓派共享文件至电脑(samba)
  3. No input file specified.问题的解决
  4. Java注解配置rest服务_Spring Boot 注解—常用注解
  5. 02-requests模块的概述
  6. 运行过程中的leader选举
  7. H.264视频RTP负载格式/NALU的类型
  8. 动画理解Dijkstra算法过程
  9. Mybatis3 (2)xml映射文件
  10. iText简介(转)
  11. mysql数据库获得时间段内数据_mysql 获取规定时间段内的统计数据
  12. android图片垂直居中,img图片在div里垂直居中的最佳解决方案
  13. oppo面经-java开发
  14. 蓝桥杯-天干地支问题
  15. 6大最常用的Java机器学习库一览
  16. 前端学习-JavaScript每日一题-数组扁平化
  17. java 控制台刷屏 dll_Java,用户刷屏检测\相似字符串检测
  18. WIN7 64位系统安装CodeWarrior 6.3及BDM驱动
  19. Spark论文思想之-基于RDD构建的模型(Shark的来龙去脉)
  20. 南方电网广东公司荣获“IT用户最佳实践案例奖”

热门文章

  1. canvas笔记-arcTo的使用及绘制弯月
  2. Java工作笔记-枚举类型反射
  3. QML工作笔记-界面登录框设置(方便其他控件居中)
  4. Spring Boot中的Profile文件
  5. python截取字符串函数substr_PHP substr():截取字符串
  6. Python杂谈: __init__.py的作用
  7. 商务英语计算机,BEC商务英语
  8. sr里简体中文的代码_常用的语言代码对照表
  9. 计算机辅助小学数学教学的研究,计算机辅助数学教学的研究
  10. python 动态编译代码,Python:在运行时动态创建函数