ZOJ 2760 How Many Shortest Path 最大流+Floyed

题意:给你一个带权有向图 问你起点到终点距离最短且边不重复的路径有多少条

思路:思路很巧秒 先Floyed处理出任意两点的最短路径 然后如果floyed[S][i] + dist[i][j] + floyed[j][T] == floyed[S][T] && dist[i][j] != INF 则保存i j 这条边 容量为1 代表只能用一次

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define CLR( a, x ) memset( a, x, sizeof a )
#define CPY( a, x ) memcpy( a, x, sizeof a )

const int maxn = 100 + 10;
const int maxe = 20000 + 10;
const int INF = 1e9;

struct Edge{
          int v, c, f;
          int next;
          Edge() {}
          Edge(int v, int c, int f, int next) : v(v), c(c), f(f), next(next) {}
};

struct ISAP{
          int n, s, t;
          int num[maxn], cur[maxn], d[maxn], p[maxn];
          int Head[maxn], cntE;
          int Q[maxn], head, tail;
          Edge edge[maxe];
          void Init(int n){
                    this -> n = n;
                    cntE = 0;
                    CLR(Head, -1);
          }
          void Add(int u, int v, int c){
                    edge[cntE] = Edge(v, c, 0, Head[u]);
                    Head[u] = cntE++;
                    edge[cntE] = Edge(u, 0, 0, Head[v]);
                    Head[v] = cntE++;
          }
          void Bfs(){
                    CLR(d, -1);
                    CLR(num, 0);
                    d[t] = 0;
                    head = tail = 0;
                    Q[tail++] = t;
                    num[0] = 1;
                    while(head != tail){
                              int u = Q[head++];
                              for(int i = Head[u]; ~i; i = edge[i].next){
                                        Edge &e = edge[i];
                                        if(~d[e.v]) continue;
                                        d[e.v] = d[u] + 1;
                                        Q[tail++] = e.v;
                                        num[d[e.v]] ++;
                              }
                    }
          }
          int Maxflow(int s, int t){
                    this -> s = s;
                    this -> t = t;
                    CPY(cur, Head);
                    Bfs();
                    int flow = 0, u = p[s] = s;
                    while(d[s] < n){
                              if(u == t){
                                        int f = INF, neck;
                                        for(int i = s; i != t; i = edge[cur[i]].v){
                                                  if(f > edge[cur[i]].c - edge[cur[i]].f){
                                                            f = edge[cur[i]].c - edge[cur[i]].f;
                                                            neck = i;
                                                  }
                                        }
                                        for(int i = s; i != t; i = edge[cur[i]].v){
                                                  edge[cur[i]].f += f;
                                                  edge[cur[i]^1].f -= f;
                                        }
                                        flow += f;
                                        u = neck;
                              }
                              int ok = 0;
                              for(int i = cur[u]; ~i; i = edge[i].next){
                                        Edge &e = edge[i];
                                        if(e.c > e.f && d[e.v] + 1 == d[u]){
                                                  ok = 1;
                                                  cur[u] = i;
                                                  p[e.v] = u;
                                                  u = e.v;
                                                  break;
                                        }
                              }
                              if(!ok){
                                        int m = n - 1;
                                        if(--num[d[u]] == 0) break;
                                        for(int i = Head[u]; ~i; i = edge[i].next){
                                                  Edge &e = edge[i];
                                                  if(e.c - e.f > 0 && m > d[e.v]){
                                                            cur[u] = i;
                                                            m = d[e.v];
                                                  }
                                        }
                                        ++num[d[u] = m + 1];
                                        u = p[u];
                              }
                    }
                    return flow;
          }

}solver;

int n, S, T;
int dist[maxn][maxn], floyed[maxn][maxn];

void input(){
          REP(i, 0, n) REP(j, 0, n){
                    scanf("%d", &dist[i][j]);
                    if(dist[i][j] == -1) dist[i][j] = INF;
                    if(i == j) dist[i][j] = 0;//太坑了 不加这句就WA到死... 有意思么...
                    floyed[i][j] = dist[i][j];
          }
          scanf("%d%d", &S, &T);
}

void solve(){
          if(S == T){
                    printf("inf\n");
                    return;
          }
          REP(k, 0, n) REP(i, 0, n) REP(j, 0, n)
                    if(floyed[i][k] != INF && floyed[k][j] != INF)
                              floyed[i][j] = min(floyed[i][j], floyed[i][k] + floyed[k][j]);
          if(floyed[S][T] == INF){
                    printf("0\n");
                    return;
          }
          solver.Init(n);
          REP(i, 0, n) REP(j, 0, n)
                    if(floyed[S][i] + dist[i][j] + floyed[j][T] == floyed[S][T] && dist[i][j] != INF)
                              solver.Add(i, j, 1);
          printf("%d\n", solver.Maxflow(S, T));
}

int main()
{
          //freopen("in.txt", "r", stdin);
          while(~scanf("%d", &n)){
                    input();
                    solve();
          }
          return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值