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 40dijkstra算法,距离相同情况下,选消费少的,记录路径即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#define inf 0x3f3f3f3f
#define MAX 501
using namespace std;
int n,m,s,d,tdis,tcost,a,b;
int edis[MAX][MAX];
int ecost[MAX][MAX];
int dis[MAX];
int cost[MAX];
int vis[MAX];
int path[MAX];
void print(int k) {
if(k == s)return;
print(path[k]);
printf(" %d",k);
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&d);
for(int i = ;i < n;i ++) {
for(int j = ;j < n;j ++)
edis[i][j] = inf;
edis[i][i] = ;
dis[i] = inf;
}
for(int i = ;i < m;i ++) {
scanf("%d%d%d%d",&a,&b,&tdis,&tcost);
edis[a][b] = edis[b][a] = tdis;
ecost[a][b] = ecost[b][a] = tcost;
}
dis[s] = ;
int t,mi;
while() {
t = -;
mi = inf;
for(int i = ;i < n;i ++) {
if(!vis[i] && dis[i] < mi)mi = dis[i],t = i;
}
if(t == -)break;
vis[t] = ;
for(int i = ;i < n;i ++) {
if(vis[i] || edis[t][i] == inf)continue;
if(mi + edis[t][i] < dis[i]) {
dis[i] = mi + edis[t][i];
cost[i] = cost[t] + ecost[t][i];
path[i] = t;
}
else if(mi + edis[t][i] == dis[i] && cost[t] + ecost[t][i] < cost[i]) {
cost[i] = cost[t] + ecost[t][i];
path[i] = t;
}
}
}
printf("%d",s);
print(d);
printf(" %d %d",dis[d],cost[d]);
}
1030 Travel Plan (30)(30 分)的更多相关文章
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- 1030 Travel Plan (30 分)(最短路径 and dfs)
#include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; int mp[N][N]; bool vi ...
- 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 ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- 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 ...
随机推荐
- ASIHTTPRequest中文入门教程全集 http://www.zpluz.com/thread-3284-1-1.html
本文转载至 目录 3 第 1 章 创建和运行请求 5 1.1. 创建一个同步请求 5 1.2. 创建一个异步请求 5 1.3. 使用程序块(blocks ) 6 1.4. 使用 ...
- 九度OJ 1335:闯迷宫 (BFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1782 解决:483 题目描述: sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫. sun的室友在帮电脑节设计 ...
- ConfigurableBeanFactory
ConfigurableBeanFactory :关系如下 在上面这样的一个关系图中可以先看下SingletonBeanRegistry的源代码: package org.springframewor ...
- 右键打开cmd
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere]@="Open cmd ...
- Linux mariadb(Mysql)的主从复制架构
mysql的主从复制架构,需要准备两台机器,并且可以通信,安装好2个mysql,保持版本一致性 mysql -v 查看数据库版本 1.准备主库的配置文件 /etc/my.cnf 写入开启主库的参数[ ...
- Java找出一组数字的最大值
形如:int [] nums = {7,2,8,9,1,12}; 解一:两两比较并记录下标,下次比较拿上次比较的最大值和上次比较的下一个进行比较,循环一次找出最大值 /** * @author 马向峰 ...
- 关于 IN UPDATE TASK
[转 http://blog.sina.com.cn/s/blog_6f74e6d50100sq57.html]更新程序必须用一个特殊的FM(update module)来实现. 1.Exportin ...
- python实例2-写一个爬虫下载小功能
主要是通过url,和re两个模块对一个网页的固定图片进行模糊匹配后下载下来. #! /usr/bin/python import re import urllib def gethtml(url): ...
- ARDUINO W5100 WebServer测试
1.直接下载官方的enternet->WebServer代码 /* Web Server A simple web server that shows the value of the anal ...
- Kattis - yoda 【字符串】
分析 给出两个串 从末尾开始对齐 每位对齐后,每一位 遍历 如果 第一串 的那位 < 第二串 的 那么 第一串的那位 就删去 如果 等于 两位 都保留 如果 大于 那么 保留 第二串的 那位 如 ...