A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
提交代码
/*
http://www.patest.cn/contests/pat-a-practise/1030
1030. Travel Plan (30)
此题用数组 把city结构体分开用变量 一直是内存超限??
换成vector 加结构体的模式 就ok 了 另外 vector 使用resize() assign() 要会
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define INF 0x6FFFFFFF
int n, m, s, d;
typedef struct edge
{
int v;
int dis;
int cos;
edge(int _v=0, int _dis=0, int _cos=0) :v(_v), dis(_dis), cos(_cos){}
};
vector<vector<edge>> dist;// 二维数组
typedef struct city{
int dis;
int fee; // 费用
int pre; // city i 的上一个节点 到时候回溯 得到结果用
city(){
dis = INF;
fee = INF;
pre = -1;
}
};
//vector<int> dis;
//vector<int> fee;
//vector<int> pre;
vector<bool> vis;
vector<city> citys;
void Dijkstra()
{
int i, j;
vis.assign(n, false);
citys.clear();
citys.resize(n); //有构造函数后 可以直接初始化
/*for (i = 0; i < n; i++)
{
citys[i].fee = INF;
citys[i].dis = INF;
}*/
/*int lens = dist[s].size();
for (i = 0; i < lens; i++)
{
edge et = dist[s][i];
citys[et.v].fee = et.cos;
citys[et.v].dis = et.dis;
}*/
vis[s] = true;
citys[s].dis = 0;
citys[s].fee = 0;
int newP = s;
while (newP != d)
{
int lenp = dist[newP].size();
for (i = 0; i < lenp;i++)
{
edge ett = dist[newP][i];
int vv = ett.v;
int diss = ett.dis;
int coss = ett.cos;
if (!vis[vv])
{
if (citys[newP].dis + diss < citys[vv].dis) // 距离小 直接更新
{
citys[vv].dis = diss + citys[newP].dis;
citys[vv].fee = citys[newP].fee + coss;
citys[vv].pre = newP;
}
else if (citys[newP].dis + diss == citys[vv].dis) // 距离相等
{
if (citys[newP].fee + coss < citys[vv].fee) // 看费用是否小
{
citys[vv].fee = citys[newP].fee + coss;
citys[vv].pre= newP;
}
}
}
}
//选择下一个节点 也就是 s到他的距离要最小
int minn = INF;
for (i = 0; i < n; i++)
{
if (!vis[i] && citys[i].dis < minn)
{
minn = citys[i].dis;
newP = i;
}
}
vis[newP] = true; // vis 更新
}// end while
}
int main()
{
//freopen("in", "r", stdin);
while (scanf("%d%d%d%d", &n, &m, &s, &d) != EOF)
{
int i;
int u, v, diss, cos;
dist.resize(n);
for (i = 0; i < m; i++)
{
scanf("%d%d%d%d", &u, &v, &diss, &cos);
dist[u].push_back(edge(v,diss,cos));
dist[v].push_back(edge(u, diss, cos));
}
Dijkstra();
vector<int> vans;
int len = 0;
int nv = d;
while (nv != -1)
{
vans.push_back(nv);
len++;
nv = citys[nv].pre;
}
for (i = len - 1; i >= 0; i--)
printf("%d ", vans[i]);
printf("%d %d", citys[d].dis, citys[d].fee); // 这里自己一直写错了 dis 和 fee
printf("\n");
}
return 0;
}
Dijkstra算法从起点出发,维持一个起点到其它点的最短距离,每次都需要更新这些距离,并加入最段距离的那个点,知道所有点都找到,有点类似prime。
需要注意结构体和数据的构造。