zoj2760:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760

题意:给你一张有向带权图,然后问你最短路径有多少条。

题解:这一题用到了网络流,一开始,我想到用找到一条最短路,然后删除这条,然后继续找,发现这样是不对。然后,看了别人的题解,发现,用网络流搞。就是把所有的最短路径的边对应的点之间建边,边的容量是1,然后跑网络流。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#define INF 100000000
using namespace std;
const int N=;
const int M=;
int mp[N][N],dist[N][N];
struct Node{
int v;
int f;
int next;
}edge[M];
int n,m,u,v,s,t,cnt,sx,ex;
int head[N],pre[N];
void init(){
cnt=;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w){
edge[cnt].v=v;
edge[cnt].f=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].f=;
edge[cnt].v=u;
edge[cnt].next=head[v];
head[v]=cnt++;
}
bool BFS(){
memset(pre,,sizeof(pre));
pre[sx]=;
queue<int>Q;
Q.push(sx);
while(!Q.empty()){
int d=Q.front();
Q.pop();
for(int i=head[d];i!=-;i=edge[i].next ){
if(edge[i].f&&!pre[edge[i].v]){
pre[edge[i].v]=pre[d]+;
Q.push(edge[i].v);
}
}
}
return pre[ex]>;
}
int dinic(int flow,int ps){
int f=flow;
if(ps==ex)return f;
for(int i=head[ps];i!=-;i=edge[i].next){
if(edge[i].f&&pre[edge[i].v]==pre[ps]+){
int a=edge[i].f;
int t=dinic(min(a,flow),edge[i].v);
edge[i].f-=t;
edge[i^].f+=t;
flow-=t;
if(flow<=)break;
} }
if(f-flow<=)pre[ps]=-;
return f-flow;
}
int solve(){
int sum=;
while(BFS())
sum+=dinic(INF,sx);
return sum;
}
int temp;
int main() {
while(~scanf("%d",&n)){
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&temp);
if(i==j)mp[i][j]=;
else if(temp==-)mp[i][j]=INF;
else
mp[i][j]=temp;
dist[i][j]=mp[i][j];
}
}
scanf("%d%d",&s,&t);
if(s!=t){
s++;t++;
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(mp[i][j]<INF&&dist[s][i]+mp[i][j]+dist[j][t]==dist[s][t])
add(i,j,);
}
}
sx=s;ex=t;
printf("%d\n",solve());
}
else
printf("inf\n");
}
return ;
}

How Many Shortest Path的更多相关文章

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

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

  2. zoj 2760 How Many Shortest Path 最大流

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

  3. The Shortest Path in Nya Graph

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

  4. hdu 3631 Shortest Path(Floyd)

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

  5. Shortest Path(思维,dfs)

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

  6. Shortest Path

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

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

  8. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  9. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  10. OSPF(Open Shortest Path First)

    1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的,所以所有厂商都可以用. OSPF的流量使用IP协议号. O ...

随机推荐

  1. VS2012执行Cocos2d-xTest案例载入失败解决方式

    编译cocos2d-x,一直提示载入失败 通过搜索,解决方式例如以下: 第一步,通过命令提示行工具进入VS安装文件夹下的%VS2012home%Common7\IDE 第二部,运行devenv.exe ...

  2. GCC 编绎选项 转

    gcc提供了大量的警告选项,对代码中可能存在的问题提出警告,通常可以使用-Wall来开启以下警告:           -Waddress -Warray-bounds (only with -O2) ...

  3. Ⅱ.AngularJS的点点滴滴--缓存

    模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...

  4. ASP.NET中常用重置数据的方法

    aspx: <asp:Repeater ID="rptProlist" runat="server" onitemdatabound="rptP ...

  5. 原生js日期时间插件鼠标点击文本框弹出日期时间表格选择日期时间

    原文出处 (这是我从互联网上搜来的,感觉能满足各方面的需求.个人感觉挺不错的,所以后期修改了一下向大家推荐!) 效果图: html代码: <!DOCTYPE html PUBLIC " ...

  6. gitlab 配置 ssh && ubuntu

    1,在你的电脑上生成密钥ssh-keygen -t rsa -C "youeamil@explode.com" 2,在 ubuntu系统中 ~/.ssh目录中生成了两个文件id_r ...

  7. 在eclipse中部署发布web项目 和 更改eclipseweb项目发布的路径

    我的工作空间:d:workspaceweb项目名称:xxx在eclipse配置完tomcat后,发布到的路径是 d:\workspace\.metadata\.plugins\org.eclipse. ...

  8. 反射+javacsv+scv文件构建资源获取

    1.反射之前已经写过,大家可以在http://www.cnblogs.com/myadmin/p/5282628.html中大概了解下.同时请大家指正. javacsv.jar:java中用来处理cs ...

  9. Object To Enum

    public static T ObjectToEnum<T>(object o) { try { return (T)Enum.Parse(typeof(T), o.ToString() ...

  10. spring quartz 多次调用

    背景:公司项目有一个定时任务,每月1号0点执行,用到了spring的定时任务.发下定时任务调用的方法执行了俩次.测试部署的客户现场不会有问题 (测试的server.xml不会变化,除非本身提供的tom ...