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 ...
随机推荐
- [kuangbin带你飞]专题一 简单搜索 - D - Fliptile
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- angluar1.8.2 PC Mail项目笔记
兼容性技术选型 前后端分离 代理gulp nginx jq+angluar1.8.2 使用级别刚刚好的相对目录,方便转移项目或者做接口代理时的切换目录 指令过滤器服务控制器书写位置 方法封装,自己写和 ...
- Django之13种必会查询
1.常见的13中查询方式(必须记住) <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> g ...
- .net core模糊查询及分页
在项目文件夹中,创建 PaginatedList类,然后用以下代码替换模板代码. using Microsoft.EntityFrameworkCore; using System; using Sy ...
- Eclipse 中安装 CDT 插件编写 C/C++
使用到的软件 1.Eclipse 开发工具 2.MinGW 编译器 一.Eclipse 中安装 CDT 插件 打开 Eclipse 插件市场 搜索 CDT,并找到如下的插件.插件的版本名字可能不太一样 ...
- 【数位DP】[LOJ10168] 恨7不成妻
还是数位DP... 状态:$f[x][val][sum]$表示当前第x位,当前数字为val,当前各位数字和为sum 观察到$val$,$sum$过大,很套路地模7即可... 每个状态存储三个要用到的值 ...
- 【JZOJ3337】wyl8899的TLE
description wyl8899今天也很刻苦的在做老师布置下来的题目! 这一天老师布置的题目是这样的: 给出两个仅含小写字母的字符串A和B,输出最大的k,使得A[1..k]是B的子串. A和B的 ...
- shell与crontab定时器的结合
crond服务 以守护进程方式在无需人工干预的情况下来处理一些列的作业指令与服务 查看服务状态 systemctl status cron.service 停止服务 systemctl stop cr ...
- php包含文件
PHP 包含文件 PHP include 和 require 语句 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容. include 和 require 语句用于在执 ...
- codeforces 1195D2-Submarine in the Rybinsk Sea
传送门:QAQQAQ 题意:自己看 思路:就是一个类似于数位DP的东西... 统计a[i]数位分解的数在每一位出现的个数,即分两种讨论: 1.位数小于当前j,则j会出现在q+i,而且计算顺序互换会计算 ...