学习书籍博客若干,为自用模板,以下所有代码都可以在最短路版题HDU - 2544 最短路中AC。
Dijkstra(堆优化)
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define MAXN 100+5
#define MAXE 10000+5
#define INF 0x3f3f3f3f
using namespace std;
struct Edge {
int v, w;
Edge() = default;
Edge(int v, int w):v(v),w(w){}
};
struct Node {
int id, dis;
Node() = default;
Node(int id, int dis):id(id),dis(dis){}
bool operator < (const Node &b) const {
return dis > b.dis;
}
};
vector<Edge> E[MAXN];
int vis[MAXN];
int dis[MAXN];
inline void init() {
memset(vis, 0, sizeof(vis));
memset(dis, INF, sizeof(dis));
for (auto &e : E) e.clear();
}
inline int djs(int src, int dst) {
priority_queue<Node> q;
dis[src] = 0;
q.push(Node(src, dis[src]));
while(!q.empty()) {
Node u = q.top();
q.pop();
if(dis[u.id] != u.dis)
continue;
for (Edge &e : E[u.id]) {
if(dis[e.v] > dis[u.id] + e.w) {
dis[e.v] = dis[u.id] + e.w;
q.push(Node(e.v, dis[e.v]));
}
}
}
return dis[dst];
}
int main()
{
int n, m, a, b, c;
while (~scanf("%d%d",&n,&m) && n+m) {
init();
for (int i=1; i<=m; ++i) {
scanf("%d%d%d",&a,&b,&c);
E[a].emplace_back(b, c);
E[b].emplace_back(a, c);
}
printf("%d\n",djs(1, n));
}
return 0;
}
Floyd
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 100+5
#define INF 0x3f3f3f3f
using namespace std;
int g[MAXN][MAXN];
inline void init()
{
memset(g, INF, sizeof(g));
}
inline int Floyd(int src, int dst)
{
// WARNING: Original graph is modified
for(int k=1; k<=MAXN; ++k)
for(int i=1; i<=MAXN; ++i)
for(int j=1; j<=MAXN; ++j)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
return g[src][dst];
}
int main()
{
int n,m,u,v,w;
while(~scanf("%d%d",&n,&m) && n+m)
{
init();
while(m--) {
scanf("%d%d%d",&u,&v,&w);
g[u][v] = g[v][u] = w;
}
printf("%d\n",Floyd(1, n));
}
return 0;
}
Bellman-Ford
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define MAXN 100+5
#define INF 0x3f3f3f3f
using namespace std;
struct Edge {
int v;
int w;
Edge(int v, int w):v(v),w(w) {}
};
vector<Edge> e[MAXN];
int dis[MAXN];
int n,m; // nodenum && edgenum
inline void init()
{
for (auto &v : e) v.clear();
memset(dis, INF, sizeof(dis));
}
inline int BellmanFord(int src, int dst)
{
dis[src] = 0;
for (int i=0; i<n-1; ++i)
for(int j=1; j<=n; ++j)
for (auto &ee : e[j])
dis[ee.v] = min(dis[ee.v], dis[j]+ee.w);
/* 判负环
for (int j=1; j<=n; ++j)
for (auto &ee : e[j])
if (dis[ee.v] > dis[j]+ee.w)
return -INF;
*/
return dis[dst];
}
int main()
{
int u,v,w;
while(~scanf("%d%d",&n,&m) && n+m) {
init();
for(int i=1; i<=m; ++i) {
scanf("%d%d%d",&u,&v,&w);
e[u].emplace_back(v, w);
e[v].emplace_back(u, w);
}
printf("%d\n",BellmanFord(1, n));
}
return 0;
}
SPFA
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 100+5
#define MAXE 10000+5
#define INF 0x3f3f3f3f
using namespace std;
int N,E;
int g[MAXN][MAXN];
bool vis[MAXN];
int dis[MAXN];
// int in[MAXN];
queue<int> q;
inline void init()
{
memset(g, 0x3f, sizeof(g));
memset(vis, 0, sizeof(vis));
// memset(in, 0, sizeof(in));
while(!q.empty()) q.pop();
}
inline int SPFA(int src, int dst)
{
memset(dis, 0x3f, sizeof(dis));
dis[src] = 0;
vis[src] = true;
q.push(src);
while(!q.empty())
{
int cur = q.front();
q.pop();
vis[cur] = false;
for(int i=1; i<=N; ++i)
{
if(dis[i] > dis[cur] + g[cur][i]) {
dis[i] = dis[cur] + g[cur][i];
if (!vis[i]) {
q.push(i);
vis[i] = true;
/* 判负环
++in[i];
if(in[i] >= N)
return -INF;
*/
}
}
}
}
return dis[dst];
}
int main()
{
int u,v,w;
while(~scanf("%d%d",&N,&E) && N+E)
{
init();
for (int i=1; i<=E; ++i) {
scanf("%d%d%d",&u,&v,&w);
g[u][v] = g[v][u] = w;
}
printf("%d\n",SPFA(1,N));
}
return 0;
}