1:Dijkstra+DFS
//错误总结
//1:不需要记录最小花费,这里就是和之前的DJ的区别所在,后面专门计算最小花费
//2:d[u]要进行更新
//3:temp.pop_back();//不需要传参数进去
//4:已知tempPath,不知道怎么求花费(找到相邻的下标进行操作)
//5:用cin输入的时候:不要写成书上那种简便写法:
//5:scanf("%d%d",&v,&n);直接用v,n作为下标进行操作,好像是不行,要进行初始化,并且还输入不进去
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n, m, s, e;
const int INF = 100000000;
const int MAXV = 510;
vector<int>pre[MAXV],path,temp;
int G[MAXV][MAXV],c[MAXV][MAXV];
int d[MAXV];//不需要记录最小花费,这里就是和之前的DJ的区别所在,后面专门计算最小花费
bool vis[MAXV] = { false};
void Dj(int v)
{
fill(d, d + MAXV, INF);
d[v] = 0;
for (int i = 0; i < n; i++)
{
int u = -1, min = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == false && d[j] < min)
{
u = j;
min = d[j];
}
}
if (u == -1)return;
vis[u] = true;
for (int k = 0; k < n; k++)
{
if (vis[k] == false && d[u] + G[u][k] < d[k])
{
//d[u]要进行更新
d[k] = d[u] + G[u][k];
pre[k].clear();
pre[k].push_back(u);
}
else if (vis[k] == false && d[u] + G[u][k] == d[k])\
{
pre[k].push_back(u);
}
}
}
}
int optvalue=INF;
void DFS(int en)
{
if (en == s)
{
temp.push_back(en);
int value = 0;
for (int i =temp.size()-1;i > 0; i--)//从后面进行遍历,就应该为i--
{
int id = temp[i], idnext = temp[i-1];
value += c[id][idnext];
}
if (value < optvalue) {
path = temp;
optvalue = value;
}
temp.pop_back();//不需要传参数进去
}
temp.push_back(en);
for (int i = 0; i < pre[en].size(); i++)
{
DFS(pre[en][i]);
}
temp.pop_back();
}
int main() {
cin >> n >> m >> s >> e;
fill(G[0], G[0] + MAXV * MAXV, INF);
fill(c[0], c[0] + MAXV * MAXV, 0);
int v, p,w,dw;
for (int i = 0; i < m; i++)
{
cin >> v >> p >> w >> dw;
G[p][v] = G[v][p]=w;
c[p][v] = c[v][p]=dw;
}
Dj(s);
DFS(e);
for (int i = path.size()-1; i >=0 ; i--)
{
cout << path[i]<<' ';
}
cout << d[e] <<' '<< optvalue << endl;
return 0;
}
2:Dijkstra算法
//错误总结:
//1:填充的时候c数组中出现:MAXV*MAXV,然后出现段错误
#include <iostream>
#include<algorithm>
using namespace std;
int n, m, s, e;
const int MAXV = 510;//开始把数组调小了
const int INF = 1000000000;
int adj[MAXV][MAXV],c[MAXV][MAXV];
int d[MAXV], cost[MAXV], pre[MAXV];
bool vis[MAXV] = { false };
void Dj(int s) {
fill(d, d + MAXV, INF);
d[s] = 0;
//初始化:又是老问题:MAXV*MAXV
fill(cost, cost + MAXV , INF);
cost[s] = 0;
for (int i = 0; i < n; i++)pre[i] = i;
for (int i = 0; i < n; i++)
{
int u = -1, min = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == false && d[j] < min)
{
u = j;
min = d[j];
}
}
if (u == -1)return;
vis[u] = true;
for (int j = 0; j < n; j++)
{
// && adj[i][u] != INF,从上面那个循环就已经知道其有路径
if (vis[j] == false && adj[u][j] != INF)
{
if (d[u] + adj[u][j] < d[j])
{
d[j] = d[u] + adj[u][j];
pre[j] = u;
cost[j] = cost[u] + c[u][j];
}
else if (d[u] + adj[u][j] == d[j]&&cost[j] > cost[u] + c[u][j])
{
pre[j] = u;
cost[j] = cost[u] + c[u][j];
}
}
}
}
}
void DFS(int vt) {
if (vt == s)
{
cout << pre[vt]<<' ';
//没有递归出口的话,一直成为死循环
return;
}
DFS(pre[vt]);
cout << vt<<' ';
}
int main()
{
cin >> n >> m >> s >> e;
int i,o,l,w;
//初始化图
fill(adj[0], adj[0] + MAXV * MAXV, INF);
for (int p = 0; p < m; p ++)
{
cin >> i >> o>>l>>w;
adj[i][o] = l;
adj[o][i] = l;
c[i][o] = w;
c[o][i] = w;
}
Dj(s);
DFS(e);
cout << d[e] <<" "<< cost[e] << endl;
return 0;
}