pat 1030

2024-02-11 16:48
文章标签 pat 1030

本文主要是介绍pat 1030,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1030. Travel Plan (30)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
 
//该题重点是存储最短路径的方法。path[j] = p;表示到达城市j的最短路径中前一个城市是p
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define INF 1 << 20int vis[510], path[510], dist[510], cost[510];
int map[510][510], mapCost[510][510];
int n, m, s, d;vector<int> Q;void dij()
{int i, j;for(i = 0; i < n; i++)dist[i] = INF;dist[s] = 0;cost[s] = 0;for(i = 0; i < n; i++){int min = INF, index = -1;for(j = 0; j < n; j++){if(vis[j] == 0 && dist[j] < min){min = dist[j];index = j;}}if(min == INF || index == d) return;vis[index] = 1;for(j = 0; j < n; j++){if(vis[j] == 0){int tmp = dist[index] + map[index][j];if(tmp < dist[j]){dist[j] = tmp;path[j] = index; //到达城市j的最短路径上的前一个城市是城市indexcost[j] = cost[index] + mapCost[index][j]; //cost[j]表示从起始的城市s到城市j的最小花费}else if(tmp == dist[j]){if(cost[j] > cost[index] + mapCost[index][j]){//如果这条路的花费高path[j] = index; //修改原先到达城市j的前一个城市是path[j]的路径,换成到达城市j的前一个城市是index的路径cost[j] = cost[index] + mapCost[index][j];}}}}}
}
int main()
{int i, j, c1, c2, di, co;while(scanf("%d%d%d%d", &n, &m, &s, &d) != EOF){for(i = 0; i <= n; i++)for(j = 0; j <= n; j++)map[i][j] = map[j][i] = INF;for(i = 1; i <= m; i++){scanf("%d%d%d%d", &c1, &c2, &di, &co);map[c1][c2] = map[c2][c1] = di;mapCost[c1][c2] = mapCost[c2][c1] = co;}memset(vis, 0, sizeof(vis));dij();Q.clear();int tmp = d;while(d != s){Q.push_back(d);d = path[d];}Q.push_back(s);for(i = Q.size() - 1; i >= 0; i--)printf("%d ", Q[i]);printf("%d %d\n", dist[tmp], cost[tmp]);}return 0;
}


这篇关于pat 1030的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/700304

相关文章

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

PAT (Advanced Level) Practice——1011,1012

1011:  链接: 1011 World Cup Betting - PAT (Advanced Level) Practice (pintia.cn) 题意及解题思路: 简单来说就是给你3行数字,每一行都是按照W,T,L的顺序给出相应的赔率。我们需要找到每一行的W,T,L当中最大的一个数,累乘的结果再乘以0.65,按照例子写出表达式即可。 同时还需要记录每一次选择的是W,T还是L

PAT (Advanced Level) Practice

1001:  题目大意: 计算 a+b 的结果,并以标准格式输出——即每三个数字一组,组之间用逗号分隔(如果数字少于四位,则不需要逗号分隔)  解析: 我们知道相加右正有负,对于样例来说 Sample Input: -1000000 9 Sample Output: -999,991 如果是从左往右,算上负号的话输出应该是-99,999,1 从右往左:-,999,991离正确

1050 String Subtraction——PAT甲级

Given two strings S1​ and S2​, S=S1​−S2​ is defined to be the remaining string after taking all the characters in S2​ from S1​. Your task is simply to calculate S1​−S2​ for any given strings. However,

1105 链表合并——PAT乙级

给定两个单链表 L1​=a1​→a2​→⋯→an−1​→an​ 和 L2​=b1​→b2​→⋯→bm−1​→bm​。如果 n≥2m,你的任务是将比较短的那个链表逆序,然后将之并入比较长的那个链表,得到一个形如 a1​→a2​→bm​→a3​→a4​→bm−1​⋯ 的结果。例如给定两个链表分别为 6→7 和 1→2→3→4→5,你应该输出 1→2→7→3→4→6→5。 输入格式: 输入首先在第一

1110 区块反转——PAT乙级

给定一个单链表 L,我们将每 K 个结点看成一个区块(链表最后若不足 K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。例如:给定 L 为 1→2→3→4→5→6→7→8,K 为 3,则输出应该为 7→8→4→5→6→1→2→3。 输入格式: 每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤105)、以及正整数 K (

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个

每日一题——Python代码实现PAT乙级1048 数字加密(举一反三+思想解读+逐步优化)五千字好文

一个认为一切根源都是“自己不够强”的INTJ 个人主页:用哲学编程-CSDN博客专栏:每日一题——举一反三Python编程学习Python内置函数 Python-3.12.0文档解读 目录 初次尝试  再次尝试 代码点评 代码结构 时间复杂度 空间复杂度 优化建议 我要更强 优化建议 完整代码及注释 时间复杂度和空间复杂度分析 进一步优化 哲学和编程思想 模块化

PAT-L1-020 帅到没朋友

L1-020 帅到没朋友 (20 分) 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。 输入格式: 输入第一行给出一个正整数N(≤100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(≤1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),I