PAT甲级——A1030 Travel Plan
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 (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); Mis 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
#include <iostream>
#include <vector>
using namespace std;
#define inf 999999999
//使用dijkstra
int N, M, S, D;
vector<int>tempPath, path;
struct Node
{
int dis = inf, w = inf;
}node;
int minW = inf;
void DFS(vector<vector<Node>>&city, vector<vector<int>>&father, int k)
{
tempPath.push_back(k);
if (k == S)
{
int tempW = ;
for (int i = tempPath.size() - ; i > ; --i)
tempW += city[tempPath[i]][tempPath[i - ]].w;
if (tempW < minW)
{
minW = tempW;
path = tempPath;
}
tempPath.pop_back();
return;
}
for (int i = ; i < father[k].size(); ++i)
DFS(city, father, father[k][i]);
tempPath.pop_back();
}
int main()
{
cin >> N >> M >> S >> D;
vector<vector<Node>>city(N, vector<Node>(N, node));
vector<vector<int>>father(N, vector<int>(, S));
for (int i = ; i < M; ++i)
{
int a, b;
cin >> a >> b >> node.dis >> node.w;
city[a][b] = city[b][a] = node;
}
vector<int>dis(N , inf);
vector<bool>visit(N, false);
dis[S] = ;
//Dijkstra
for (int i = ; i < N; ++i)
{
int index = -, minDis = inf;
for (int j = ; j < N; ++j)
{
if (visit[j]== false && minDis > dis[j])
{
index = j;
minDis = dis[j];
}
}
if (index == -)break;
visit[index] = true;
for (int j = ; j < N; ++j)
{
if (visit[j] == false && city[index][j].dis < inf)
{
if (dis[j] > dis[index] + city[index][j].dis)
{
dis[j] = dis[index] + city[index][j].dis;
father[j][] = index;
}
else if(dis[j] == dis[index] + city[index][j].dis)
father[j].push_back(index);
}
}
}
DFS(city, father, D);
for (int i = path.size() - ; i >= ; --i)
cout << path[i] << " ";
cout << dis[D] << " " << minW;
return ;
}
PAT甲级——A1030 Travel Plan的更多相关文章
- 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 ...
- PAT 甲级 1030 Travel Plan
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- A1030. Travel Plan
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- PAT_A1030#Travel Plan
Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
随机推荐
- 如何打开rdb文件
后缀名是RDB用什么软件打开不能用记事本打开后是乱码不知用什么软件写入的... RDB文件是QQ2009SP以后的替代DB文件的一种新的文件格式,是一种数据库文件请下载 百度搜索下载:rdb打包解包工 ...
- VS2010-MFC(文档、视图和框架:概述)
转自:http://www.jizhuomi.com/software/221.html 前面几节讲了菜单.工具栏和状态栏的使用,本节开始将为大家讲解文档.视图和框架的知识. 文档.视图和框架简介 在 ...
- vue mounted组件的使用
1.钩子函数 钩子函数是Windows消息处理机制的一部分,通过设置“钩子”,应用程序可以在系统级对所有消息.事件进行过滤,访问在正常情况下无法访问的消息.钩子的本质是一段用以处理系统消息的程序,通 ...
- fastjson 过滤不需要序列化的属性
JSON JSON英文全称为JavaScriptObject Natation,采用key:value键值对的方式存贮数据,与xml格式相比,JSON是一种轻量级的数据交换格式:不要被JavaScri ...
- c++ const的用法
1.修饰成员变量 int value=0; int *p=&value; const int *p_c=&value;//指针指向常量,但是指针所指向的地址可以修改(int const ...
- Java 11 发布计划来了,已确定 3个 新特性!!
Oracle 已经发布了 Java Development Kit 10,下一个版本 JDK 11 也在准备之中了.按照 Java 新的版本发布标准,Java 11 将在 6 个月后到来,现在它还只有 ...
- easyui treegrid的使用示例
一.前端: <div id="tbList" fit="true"></div> $(function () { $("#tb ...
- BezierCode 工具使用
概要 今天无意间看到一个视频,发现了一款绘画Bezier 图形绘制并自动生成OC代码的神器, 因此马上先记录下. 之前一直很纠结如果程序员自己去绘制图片,久那么使用bezier 自己去画吗? 答案是: ...
- 【JZOJ2288】【BZOJ1898】【luoguP2579】沼泽鳄鱼
description 潘塔纳尔沼泽地号称世界上最大的一块湿地,它地位于巴西中部马托格罗索州的南部地区.每当雨季来临,这里碧波荡漾.生机盎然,引来不少游客. 为了让游玩更有情趣,人们在池塘的中央建设了 ...
- 新书《iOS应用逆向工程:分析与实战》
前无古人!小白福音!国内第一本iOS应用逆向工程类图书<iOS应用逆向工程:分析与实战>就要空降啦~! 你是否曾因应用上线的第一天即遭破解而无奈苦恼,想要加以防范,却又束手无策? 你是否曾 ...