题意:
求商店到操场的最短路径,无向图。
题解:
建图+dijkstra/ bellman_ford/ floyd/ spfa
因为这里的数据量很小,并且没有负环,所以四种算法都可以用。
dijkstar —— O(mlogn) 适合于没有负环的情况
bellman_Ford ——O(nm) 可以处理负环
Floyd ——O(n^3) 适合多源最短路
Spfa ——O(nm) (实际运行比这个小) 可以处理负环
AC代码:
dijkstra:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int INF = 0x3f3f3f3f;
int n,m;
int d[maxn];
struct edge
{
int to,cost;
edge(int u,int d) : to(u),cost(d) {}
};
vector <edge> G[maxn];
void input()
{
for(int i = 0; i <= n; i++ )
G[i].clear();
for(int i = 0; i < m; i++)
{
int u,v,dist;
scanf("%d%d%d",&u,&v,&dist);
G[u].push_back(edge(v,dist));
G[v].push_back(edge(u,dist));
}
}
void dijkstra()
{
memset(d,INF,sizeof(d));
d[1] = 0;
priority_queue < P,vector<P>,greater<P> > Q;//P.first 是距离,.P.second 是顶点
Q.push(P(0,1));
while(!Q.empty())
{
P x = Q.top();
Q.pop();
int dist = x.first;
int u = x.second;
if(d[u] < dist) continue;//排除u相同时, dist不同的情况。
for(int i = 0; i < G[u].size(); i++)
{
edge e = G[u][i];
if(d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u]+ e.cost;
Q.push(P(d[e.to],e.to));
}
}
}
return ;
}
void output()
{
printf("%d\n",d[n]);
}
int main()
{
while(scanf("%d%d",&n,&m) && n && m)
{
input();
dijkstra();
output();
}
return 0;
}
Bellman_Ford:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int maxm = 1e4 + 100;
const int INF = 0x3f3f3f3f;
int n,m;
int d[maxn];
struct edge {
int from,to,cost;
};
edge es[maxm*2];
void Bellman_Ford()
{
memset(d,INF,sizeof(d));
d[1] = 0;
for(int i = 1; i < n; i++)
{
for(int j = 1; j <= 2*m; j++)
{
edge e = es[j];
if(d[e.from] < INF && d[e.to] > d[e.from] + e.cost) d[e.to] = d[e.from] + e.cost;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)&& n && m)
{
int j = 1;
for(int i = 1; i <= m; i++)
{
int u,v,dist;
scanf("%d%d%d",&u,&v,&dist);
edge e1,e2;
e1.from = u,e1.to = v,e1.cost = dist;
e2.from = v,e2.to = u,e2.cost = dist;
es[j++] = e1;
es[j++] = e2;
}
Bellman_Ford();
printf("%d\n",d[n]);
}
}
Floyd:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int maxm = 1e4 + 100;
const int INF = 0x3f3f3f3f;
int n,m;
int mp[maxn][maxn];
void Floyd()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);
}
int main()
{
while(scanf("%d%d",&n,&m)&& n && m)
{
memset(mp,INF,sizeof(mp));
int j = 1;
for(int i = 1; i <= m; i++)
{
int u,v,dist;
scanf("%d%d%d",&u,&v,&dist);
mp[u][v] = dist;
mp[v][u] = dist;
}
Floyd();
printf("%d\n",mp[1][n]);
}
return 0;
}
Spfa:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair <int,int> P;
const int maxn = 100+10;
const int maxm = 1e4 + 100;
const int INF = 0x3f3f3f3f;
int n,m;
int d[maxn];
int cnt[maxn];
bool inq[maxn];
struct edge
{
int to,cost;
};
vector <edge> G[maxn];
bool Spfa()
{
memset(d,INF,sizeof(d));
memset(cnt,0,sizeof(cnt));
memset(inq,false,sizeof(inq));
queue <int> Q;
Q.push(1);
d[1] = 0;
cnt[1] = 1;
inq[1] = true;
while(!Q.empty())
{
int t = Q.front();
Q.pop();
inq[t] = false;
for(int i = 0; i < G[t].size(); i++)
{
edge e = G[t][i];
if(d[e.to] > d[t] + e.cost)
{
d[e.to] = d[t] + e.cost;
if(!inq[e.to]) {
if(++cnt[e.to] > n) return false;
Q.push(e.to);
inq[e.to] = true;
}
}
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)&& n && m)
{
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 1; i <= m; i++)
{
int u,v,dist;
scanf("%d%d%d",&u,&v,&dist);
edge e1,e2;
e1.to = v, e1.cost = dist;
e2.to = u, e2.cost = dist;
G[u].push_back(e1);
G[v].push_back(e2);
}
if(Spfa())
printf("%d\n",d[n]);
}
return 0;
}