HDOJ-3416(最大流+最短路+ISAP算法+向前星dijikstra算法+如何判断一条边是否在最短路中)

Marriage Match IV

HDOJ-3416

  • 这题的题意就是要找两点之间最短路的路径个数,而且边不能重复。
  • 最大流和最短路的结合。首先正向和反向建图,再跑两遍dijikstra。到这里就求出来起点到某一点的最短路以及某一点到终点的最短路。
  • 还有一个关键的公式就是如何判断一条边是否在最短路中:如果起点到该边的起点的最短距离加上该边的终点到终点的最短距离再加上该边的长度等于起点到终点的最短路,那该边就在最短路中。
  • 还有一个需要注意的地方就是最大流算法的选用,如果需用EK算法,时间复杂度和边数成2次方关系。所以,这里选用ISAP高级算法。但也给出EK算法的求解过程。

ISAP算法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int INF=0X3F3F3F3F;
const int maxn=1003;
const int maxm=100005;
int n,m;
int map[maxn][maxn];
int d[2][maxn];//最短路
int st,ed;
struct Edge {
  int from, to, cap, flow;
  Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};

bool operator<(const Edge& a, const Edge& b) {
  return a.from < b.from || (a.from == b.from && a.to < b.to);
}

struct ISAP {
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool vis[maxn];
  int d[maxn];
  int cur[maxn];
  int p[maxn];
  int num[maxn];

  void AddEdge(int from, int to, int cap) {
    edges.push_back(Edge(from, to, cap, 0));
    edges.push_back(Edge(to, from, 0, 0));
    m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
  }

  bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(t);
    vis[t] = 1;
    d[t] = 0;
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (int i = 0; i < G[x].size(); i++) {
        Edge& e = edges[G[x][i] ^ 1];
        if (!vis[e.from] && e.cap > e.flow) {
          vis[e.from] = 1;
          d[e.from] = d[x] + 1;
          Q.push(e.from);
        }
      }
    }
    return vis[s];
  }

  void init(int n) {
    this->n = n;
    for (int i = 0; i < n; i++) G[i].clear();
    edges.clear();
  }

  int Augment() {
    int x = t, a = INF;
    while (x != s) {
      Edge& e = edges[p[x]];
      a = min(a, e.cap - e.flow);
      x = edges[p[x]].from;
    }
    x = t;
    while (x != s) {
      edges[p[x]].flow += a;
      edges[p[x] ^ 1].flow -= a;
      x = edges[p[x]].from;
    }
    return a;
  }

  int Maxflow(int s, int t) {
    this->s = s;
    this->t = t;
    int flow = 0;
    BFS();
    memset(num, 0, sizeof(num));
    for (int i = 0; i < n; i++) num[d[i]]++;
    int x = s;
    memset(cur, 0, sizeof(cur));
    while (d[s] < n) {
      if (x == t) {
        flow += Augment();
        x = s;
      }
      int ok = 0;
      for (int i = cur[x]; i < G[x].size(); i++) {
        Edge& e = edges[G[x][i]];
        if (e.cap > e.flow && d[x] == d[e.to] + 1) {
          ok = 1;
          p[e.to] = G[x][i];
          cur[x] = i;
          x = e.to;
          break;
        }
      }
      if (!ok) {
        int m = n - 1;
        for (int i = 0; i < G[x].size(); i++) {
          Edge& e = edges[G[x][i]];
          if (e.cap > e.flow) m = min(m, d[e.to]);
        }
        if (--num[d[x]] == 0) break;
        num[d[x] = m + 1]++;
        cur[x] = 0;
        if (x != s) x = edges[p[x]].from;
      }
    }
    return flow;
  }
}ek;
struct edge{
    int to;
    int cost;
    int next;
};
int head[maxn];
int heads[maxn];
edge ma[maxm];
edge mas[maxm];
struct node{
    int dis;
    int to;
    bool operator<(const node& t)const{
        return dis>t.dis;
    }
};
void dijikstra(int s,int f,int *head,edge ma[]){
    for(int i=1;i<=n;i++){
        d[f][i]=INF;
    }
    d[f][s]=0;
    priority_queue<node> q;
    q.push({0,s});
    while(!q.empty()){
        node now=q.top();
        q.pop();
        int v=now.to;
        int dis=now.dis;
        if(d[f][v]<dis){
            continue;
        }
        for(int i=head[v];i!=-1;i=ma[i].next){
            int u=ma[i].to;
            //cout<<u<<endl;
            int cost=ma[i].cost;
            if(d[f][u]>d[f][v]+cost){
                d[f][u]=d[f][v]+cost;
                q.push({d[f][u],u});
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        memset(head,-1,sizeof(head));
        memset(heads,-1,sizeof(heads));
        for(int i=0;i<m;i++){
            int a,b,c;
            cin>>a>>b>>c;
            if(a==b)
                continue;
            ma[i].to=b;
            ma[i].cost=c;
            ma[i].next=head[a];
            head[a]=i;

            mas[i].to=a;
            mas[i].cost=c;
            mas[i].next=heads[b];
            heads[b]=i;
        }
        cin>>st>>ed;
        dijikstra(st,0,head,ma);
        dijikstra(ed,1,heads,mas);
        //cout<<d[0][ed]<<endl;
        //cout<<d[1][st]<<endl;
        int final=d[0][ed];
        if(final==INF){
            cout<<0<<endl;
            continue;
        }
        ek.init(n);
        for(int i=1;i<=n;i++){
            for(int j=head[i];j!=-1;j=ma[j].next){
                if(d[0][i]+d[1][ma[j].to]+ma[j].cost==final)
                    ek.AddEdge(i,ma[j].to,1);
            }
        }
        cout<<ek.Maxflow(st,ed)<<endl;
    }
    return 0;
}

EK算法

struct edge{
    int to;
    int cost;
    int next;
};
int head[maxn];
int heads[maxn];
edge ma[maxm];
edge mas[maxm];
struct node{
    int dis;
    int to;
    bool operator<(const node& t)const{
        return dis>t.dis;
    }
};
void dijikstra(int s,int f,int *head,edge ma[]){
    for(int i=1;i<=n;i++){
        d[f][i]=INF;
    }
    d[f][s]=0;
    priority_queue<node> q;
    q.push({0,s});
    while(!q.empty()){
        node now=q.top();
        q.pop();
        int v=now.to;
        int dis=now.dis;
        if(d[f][v]<dis){
            continue;
        }
        for(int i=head[v];i!=-1;i=ma[i].next){
            int u=ma[i].to;
            //cout<<u<<endl;
            int cost=ma[i].cost;
            if(d[f][u]>d[f][v]+cost){
                d[f][u]=d[f][v]+cost;
                q.push({d[f][u],u});
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        memset(head,-1,sizeof(head));
        memset(heads,-1,sizeof(heads));
        for(int i=0;i<m;i++){
            int a,b,c;
            cin>>a>>b>>c;
            if(a==b)
                continue;
            ma[i].to=b;
            ma[i].cost=c;
            ma[i].next=head[a];
            head[a]=i;

            mas[i].to=a;
            mas[i].cost=c;
            mas[i].next=heads[b];
            heads[b]=i;
        }
        cin>>st>>ed;
        dijikstra(st,0,head,ma);
        dijikstra(ed,1,heads,mas);
        //cout<<d[0][ed]<<endl;
        //cout<<d[1][st]<<endl;
        int final=d[0][ed];
        if(final==INF){
            cout<<0<<endl;
            continue;
        }
        ek.init(n);
        for(int i=1;i<=n;i++){
            for(int j=head[i];j!=-1;j=ma[j].next){
                if(d[0][i]+d[1][ma[j].to]+ma[j].cost==final)
                    ek.AddEdge(i,ma[j].to,1);
            }
        }
        cout<<ek.Maxflow(st,ed)<<endl;
    }
    return 0;
}
posted @ 2019-09-05 16:51  Garrett_Wale  阅读(192)  评论(0编辑  收藏  举报