hdu 1874 畅通工程(spfa 邻接矩阵 邻接表)

题目链接

畅通工程,可以用dijkstra算法实现。

听说spfa很好用,来水一发

邻接矩阵实现:

#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define mem(arr, num) memset(arr, 0, sizeof(arr))
#define _for(i, a, b) for (int i = a; i <= b; i++)
#define __for(i, a, b) for (int i = a; i >= b; i--)
#define IO                     \
  ios::sync_with_stdio(false); \
  cin.tie();                  \
  cout.tie();
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
;
const ll mod = 1000000007LL;
 + ;
int mp[N][N], mp1[N][N];
int dis[N], vis[N];
int V, E;
void spfa(int start) {
  ; i <= V; i++) dis[i] = inf;
  dis[start] = ;
  vis[start] = ;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    vis[v] = ;
    ; i <= mp1[v][]; i++) {
      if (dis[mp1[v][i]] > dis[v] + mp[v][mp1[v][i]]) {
        dis[mp1[v][i]] = dis[v] + mp[v][mp1[v][i]];
        ) q.push(mp1[v][i]), vis[mp1[v][i]] = ;
      }
    }
  }
}
int main() {
  int s, e, value;
  while (cin >> V >> E) {
    mem(mp,); mem(mp1,); mem(vis,);
    _for(i, , E) {
      cin >> s >> e >> value;
       && mp[s][e] < value) continue;
      mp1[s][]++;
      mp1[s][mp1[s][]] = e;
      mp[s][e] = value;  //记录mp度并且记录每个度的点的下标。
      mp1[e][]++;
      mp1[e][mp1[e][]] = s;
      mp[e][s] = value;
    }
    int start, _end;
    cin >> start >> _end;
    spfa(start);
    if (dis[_end] < inf)
      cout << dis[_end] << endl;
    else
      cout << "-1" << endl;
  }
  ;
}

邻接表实现:

#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define mem(arr, num) memset(arr, 0, sizeof(arr))
#define _for(i, a, b) for (int i = a; i <= b; i++)
#define __for(i, a, b) for (int i = a; i >= b; i--)
#define IO                     \
  ios::sync_with_stdio(false); \
  cin.tie();                  \
  cout.tie();
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
;
const ll mod = 1000000007LL;
 + ;
 << ;
int mp[N][N], mp1[N][N];
int dis[N], vis[N];
int V, E;
int first[N];
;
struct edge {
  int point, next, value;
} e[MAXN];
void add(int u, int v, int w) {
  e[num].point = v;
  e[num].next = first[u];
  e[num].value = w;
  first[u] = num++;
}
void spfa(int start) {
  ; i <= V; i++) dis[i] = inf;
  dis[start] = ;
  vis[start] = ;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    vis[v] = ;
    ; tmp = e[tmp].next) {
      if (dis[e[tmp].point] > dis[v] + e[tmp].value) {
        dis[e[tmp].point] = dis[v] + e[tmp].value;
        , q.push(e[tmp].point);
      }
    }
  }
}
int main() {
  int s, e, value;
  while (cin >> V >> E) {
    num = ;
    mem(vis, );
    int u, v, w;
    _for(i, , V) { first[i] = -; }
    _for(i, , E) {
      cin >> u >> v >> w;
      add(u, v, w);
      add(v, u, w);
    }
    int start, _end;
    cin >> start >> _end;
    spfa(start);
    if (dis[_end] < inf)
      cout << dis[_end] << endl;
    else
      cout << "-1" << endl;
  }
  ;
}

vector存储:

#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define mem(arr, num) memset(arr, 0, sizeof(arr))
#define _for(i, a, b) for (int i = a; i <= b; i++)
#define __for(i, a, b) for (int i = a; i >= b; i--)
#define IO                     \
  ios::sync_with_stdio(false); \
  cin.tie();                  \
  cout.tie();
using namespace std;
typedef long long ll;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
;
const ll mod = 1000000007LL;
 + ;
 << ;
int dis[N], vis[N];
int V, E;
;
struct edge{

  int point, value;
  edge() {}
  edge(int _p,int _v) {point = _p, value = _v;}
};
vector <edge> e[N];
void spfa(int st) {
  ; i <= V; i++) dis[i] = INF;
  dis[st] = ;
  vis[st] = ;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    vis[v] = ;
    vector <edge> :: iterator itor = e[v].begin();
    for( ; itor != e[v].end(); itor++) {
      if(dis[(*itor).point] > dis[v] + (*itor).value) {
        dis[(*itor).point] = dis[v] + (*itor).value;
        , q.push((*itor).point);
      }
    }
    /*for(int i = 0; i < e[v].size(); i++) {
      if(dis[e[v][i].point] > dis[v] + e[v][i].value) {
        dis[e[v][i].point] = dis[v] + e[v][i].value;
        if(!vis[e[v][i].point]) vis[e[v][i].point] = 1, q.push(e[v][i].point);
      }
    }*/
  }
}
int main() {
  while (~scanf("%d%d",&V,&E)) {
    num = ;
    mem(vis, );
    int u, v, w;
    _for(i, , V) { e[i].clear(); }
    _for(i, , E) {
      scanf("%d%d%d",&u,&v,&w);
      e[u].push_back(edge(v,w));
      e[v].push_back(edge(u,w));
    }
   int st, ed;
    cin >> st >> ed;
    spfa(st);
    if (dis[ed] < INF)
      cout << dis[ed] << endl;
    else
      cout << "-1" << endl;
  }
  ;
}
上一篇:基于AOE网的关键路径的求解


下一篇:MySql 5.7.20安装