1030 Travel Plan (30)(30 分)

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

题目大意:给出起点和终点,找出起点和终点之间的所有最短路径,并且cost最小。每条边有两个属性,一个是距离,一个是花费。

// 这个就是迪杰斯特拉裸题,我一定要做出来,见过多少次这样的题了。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 999999
int edge[][];
int cost[][];
int dist[],co[];
bool vis[];
int pre[];
vector<int> path;
int main() {
int n,m,s,d;
cin>>n>>m>>s>>d;
int f,t,e,c;
fill(cost[],cost[]+*,INF);//二维数组必须用cost[0],而不能用cost。
fill(edge[],edge[]+*,INF);//这么多初始化有点烦。
for(int i=;i<m;i++){
cin>>f>>t>>e>>c;
edge[f][t]=edge[t][f]=e;
cost[f][t]=cost[t][f]=c;
}
fill(dist,dist+n,INF);
fill(co,co+n,INF);
fill(pre,pre+n,);
for(int i=;i<n;i++){
if(edge[s][i]!=)
dist[i]=edge[s][i];//初始化
if(cost[s][i]!=)
co[i]=cost[s][i];
}
//选出距离s最近的点。 dist[s]=;
vis[s]=true;
pre[s]=-;
int u=-,minn=INF;
for(int i=;i<n;i++){
u=-,minn=INF;
for(int j=;j<n;j++){
if(!vis[j]&&dist[j]<minn){
minn=dist[j];
u=j;
}
}
if(u==-||u==d)break;
vis[u]=true;
for(int j=;j<n;j++){
if(!vis[j]){
if(dist[j]>dist[u]+edge[u][j]){
dist[j]=dist[u]+edge[u][j];
pre[j]=u;
co[j]=co[u]+cost[u][j];
}else if(dist[j]==dist[u]+edge[u][j]){
if(co[j]>co[u]+cost[u][j]){
pre[j]=u;
co[j]=co[u]+cost[u][j];
}
}
}
}
}
path.push_back(d);
u=d;
while(pre[u]!=-){
path.push_back(pre[u]);
u=pre[u];
}//s不用再单独push了。
for(int i=path.size()-;i>=;i--){
cout<<path[i]<<" ";
}
cout<<dist[d]<<" "<<co[d]; return ;
}

//第一次我是这么写的,但是提示内存超限。需要改进啊。原来考点在这个地方啊,又是考内存。内存超限:您的程序使用了超过限制的内存。case通过率为30.00%

都改成了vector加上resize,也不行,也是超时。

//代码来自:https://www.liuchuo.net/archives/2369      十分值得学习,原来用dfs即可遍历。

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n, m, s, d;
int e[][], dis[], cost[][];
vector<int> pre[];//四个二维,一个一维
bool visit[];
const int inf = ;
vector<int> path, temppath;
int mincost = inf;
void dfs(int v) {//原来可以用向量来保存前驱,以前想到过但是不会实现。原来使用dfs.
temppath.push_back(v);
if(v == s) {
int tempcost = ;
for(int i = temppath.size() - ; i > ; i--) {
int id = temppath[i], nextid = temppath[i-];
tempcost += cost[id][nextid];
}
if(tempcost < mincost) {
mincost = tempcost;
path = temppath;//直接对一个向量赋值,即可。
}
temppath.pop_back();
return ;
}
for(int i = ; i < pre[v].size(); i++)
dfs(pre[v][i]);
temppath.pop_back();//最终把自己弹了出来。
}
int main() {
fill(e[], e[] + * , inf);
fill(dis, dis + , inf);
scanf("%d%d%d%d", &n, &m, &s, &d);
for(int i = ; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
scanf("%d", &e[a][b]);
e[b][a] = e[a][b];
scanf("%d", &cost[a][b]);
cost[b][a] = cost[a][b];
}
pre[s].push_back(s);//s的前驱是s自己。
dis[s] = ;
for(int i = ; i < n; i++) {
int u = -, minn = inf;
for(int j = ; j < n; j++) {
if(visit[j] == false && dis[j] < minn) {
u = j;
minn = dis[j];
}
}
if(u == -) break;
visit[u] = true;
for(int v = ; v < n; v++) {
if(visit[v] == false && e[u][v] != inf) {
if(dis[v] > dis[u] + e[u][v]) {
dis[v] = dis[u] + e[u][v];
pre[v].clear();
pre[v].push_back(u);
} else if(dis[v] == dis[u] + e[u][v]) {
pre[v].push_back(u);
}
}
}
}
dfs(d);
for(int i = path.size() - ; i >= ; i--)
printf("%d ", path[i]);
printf("%d %d", dis[d], mincost);
return ;
}

//我也不知道我改了什么,把自己的代码修好了。就最后循环出路径的while循环修改了,其他都没动,开心。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 99999
int edge[][];
int cost[][];
int dist[],co[];
bool vis[];
int pre[];
vector<int> path;
//两个二维矩阵,4个一维矩阵,一个向量。
int main() {
int n,m,s,d;
cin>>n>>m>>s>>d;
int f,t,e,c;
fill(cost[],cost[]+*,INF);//二维数组必须用cost[0],而不能用cost。
fill(edge[],edge[]+*,INF);//这么多初始化有点烦。
for(int i=;i<m;i++){
cin>>f>>t>>e>>c;
edge[f][t]=edge[t][f]=e;
cost[f][t]=cost[t][f]=c;
}
fill(dist,dist+n,INF);
fill(co,co+n,INF);
fill(pre,pre+n,-);//要全初始化为-1
//选出距离s最近的点。
dist[s]=;
co[s]=;
int u,minn;
for(int i=;i<n;i++){
u=-,minn=INF;
for(int j=;j<n;j++){
if(!vis[j]&&dist[j]<minn){
minn=dist[j];
u=j;
}
}
if(u==-||u==d)break;
vis[u]=true;
for(int j=;j<n;j++){
if(!vis[j]&&edge[u][j]!=INF){
if(dist[j]>dist[u]+edge[u][j]){
dist[j]=dist[u]+edge[u][j];
pre[j]=u;
co[j]=co[u]+cost[u][j];
}else if(dist[j]==dist[u]+edge[u][j]){
if(co[j]>co[u]+cost[u][j]){
pre[j]=u;
co[j]=co[u]+cost[u][j];
}
}
}
}
}
u=d;
while(u!=-){
path.push_back(u);
u=pre[u];
}//s不用再单独push了。
for(int i=path.size()-;i>=;i--){
cout<<path[i]<<" ";
}
cout<<dist[d]<<" "<<co[d]; return ;
}

PAT 1030 Travel Plan[图论][难]的更多相关文章

  1. PAT 1030 Travel Plan

    #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #inc ...

  2. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  3. 1030 Travel Plan (30 分)

    1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...

  4. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  5. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  6. PAT (Advanced Level) 1030. Travel Plan (30)

    先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...

  7. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

  8. PAT 甲级 1030 Travel Plan

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...

  9. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

随机推荐

  1. Qt编写百度离线版人脸识别+比对+活体检测

    在AI技术发展迅猛的今天,很多设备都希望加上人脸识别功能,好像不加上点人脸识别功能感觉不够高大上,都往人脸识别这边靠,手机刷脸解锁,刷脸支付,刷脸开门,刷脸金融,刷脸安防,是不是以后还可以刷脸匹配男女 ...

  2. 整理一系列优秀的Android开发源码

    转:http://www.cnblogs.com/feifei1010/archive/2012/09/12/2681527.html 游戏类: 一.15个Android游戏源码(是以andengin ...

  3. 编译安装的gitlab8.x如何修改时区设置

    编译安装的gitlab 8.x版本默认的时区是UTC,在页面上显示的时间默认是零时区的区时,安装完成之后,如果页面上显示的时间比北京时间少了8个小时,则需要修改一下时区 把gitlab.yml文件中的 ...

  4. httpWebRequest获取流和WebClient的文件抓取

    httpWebRequest获取流和WebClient的文件抓取 昨天写一个抓取,遇到了一个坑,就是在获取网络流的时候,人为的使用了stream.Length来获取流的长度,获取的时候会抛出错误,查了 ...

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. mysql概要(二)类型(数值型,字符型,时间类型

    1.mysql数值型范围 tinyint可选属性 tinyint(N) unsigned zerofill N:表示显示长度,与zerofill配合使用,即长度不够用0填充,并且自动变成无符号的数,N ...

  7. Twig---基本使用

    三种特殊语法: {{ … }}   “说些什么”:输出一个变量值或者一个表达式的结果到模板.如:{{ item.username }}. twig也包含filters,它可以在模板渲染之前改变输出内容 ...

  8. SSH教程从零打造在线网盘系统前言&目录

    本系列教程内容提要 本系列教程是一个学习教程,是关于Java工程师的SSH(Struts2+Spring+Hibernate)系列教程,本教程将会分为四个部分和大家一同打造一个在线网盘系统,由于教程是 ...

  9. CCCC L2-008. 最长对称子串

    https://www.patest.cn/contests/gplt/L2-008 题解:想法是扫一遍string,将每一个s[i]作为对称轴,写一个判定函数:不断向两边延伸直到不是回文串为止. ...

  10. wordpress设置固定链接无效的解决办法

    声明:本人用的是Ubuntu 10.04 LAMP服务 以下内容是针对在Apache服务器下Wordpress修改固定链接出错无效的解决办法: 如果改了固定链接以后出问题,请查看Wordpress根目 ...