Source:

PAT A1111 Online Map (30 分)

Description:

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

Keys:

Attention:

  • 其实就是求两次最短路径,代码code一遍再copy一遍,考试的时候怎么快怎么来,优化代码反而容易出错

Code:

 /*
Data: 2019-08-18 21:30:18
Problem: PAT_A1111#Online Map
AC: 42:06 题目大意:
给出当前位置和目的地,给出最短路径和最快路径
输入:
第一行给出,结点数N,边数M
接下来M行,v1,v2,单/双向,距离,时间
最后一行,起点,终点
输出:
最短距离,输出路径,不唯一则选择最快的
最短时间,输出路径,不唯一则选择经过结点最少的
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e3,INF=1e9;
int grap[M][M],cost[M][M],vis[M],d[M],c[M];
int n,m,v1,v2,one,optTime=INF,optLeth=INF;
vector<int> preL[M],preT[M],optL,pathL,optT,pathT; void DijskraL(int s)
{
fill(vis,vis+M,);
fill(d,d+M,INF);
d[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && d[j]<Min)
{
Min = d[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v] < d[v])
{
preL[v].clear();
preL[v].push_back(u);
d[v] = d[u]+grap[u][v];
}
else if(d[u]+grap[u][v]==d[v])
preL[v].push_back(u);
}
}
}
} void DFSL(int s)
{
if(s == v1)
{
pathL.push_back(v1);
int time=,len=pathL.size();
for(int i=; i<len; i++)
time += cost[pathL[i]][pathL[i-]];
if(time < optTime)
{
optTime = time;
optL = pathL;
}
pathL.pop_back();
return;
}
pathL.push_back(s);
for(int i=; i<preL[s].size(); i++)
DFSL(preL[s][i]);
pathL.pop_back();
} void DijskraT(int s)
{
fill(vis,vis+M,);
fill(c,c+M,INF);
c[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && c[j]<Min)
{
Min = c[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && cost[u][v]!=INF)
{
if(c[u]+cost[u][v] < c[v])
{
preT[v].clear();
preT[v].push_back(u);
c[v] = c[u] + cost[u][v];
}
else if(c[u]+cost[u][v]==c[v])
preT[v].push_back(u);
}
}
}
} void DFST(int s)
{
if(s == v1)
{
pathT.push_back(v1);
if(pathT.size() < optLeth)
{
optLeth = pathT.size();
optT = pathT;
}
pathT.pop_back();
return;
}
pathT.push_back(s);
for(int i=; i<preT[s].size(); i++)
DFST(preT[s][i]);
pathT.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
fill(cost[],cost[]+M*M,INF); scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d%d", &v1,&v2,&one);
scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
if(!one)
{
grap[v2][v1]=grap[v1][v2];
cost[v2][v1]=cost[v1][v2];
}
}
scanf("%d%d", &v1,&v2);
DijskraL(v1);
DFSL(v2);
DijskraT(v1);
DFST(v2);
if(optT == optL)
{
printf("Distance = %d; Time = %d: %d", d[v2],c[v2],optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
}
else
{
printf("Distance = %d: %d", d[v2], optL[optL.size()-]);
for(int i=optL.size()-; i>=; i--)
printf(" -> %d", optL[i]);
printf("\nTime = %d: %d", c[v2], optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
} return ;
}

PAT_A1111#Online Map的更多相关文章

  1. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  2. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  3. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  4. Java基础Map接口+Collections

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  5. 多用多学之Java中的Set,List,Map

            很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...

  6. Java版本:识别Json字符串并分隔成Map集合

    前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...

  7. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  8. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  9. MapReduce剖析笔记之五:Map与Reduce任务分配过程

    在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...

随机推荐

  1. awr ash addm

    awr ash addm awr addm :基于快照的ash :单独,每秒采样 dbtime=db cpu + wait 柜员忙碌的时间=A做业务的时间+B做业务的时间等待时间=B等待A做业务的时间 ...

  2. Bran的内核开发指南_中文版

    http://www.cnblogs.com/liloke/archive/2011/12/21/2296004.html 最近在看<orange’s>一书,有点想自己写一个轻量级OS的想 ...

  3. jQuery Validate 使用

    jQuery Validate 使用 <script src="js/b/js/jquery.validate.js"></script> <styl ...

  4. 《Android源代码设计模式解析与实战》读书笔记(十八)

    第十八章.代理模式 代理模式也称托付模式,是结构型设计模式之中的一个.是应用广泛的模式之中的一个. 1.定义 为其它对象提供一种代理以控制对这个对象的訪问. 2.使用场景 当无法或不想直接訪问某个对象 ...

  5. Windows移动开发(五)——初始XAML

    关于详细的基本功就先说这么多.后面遇到再补充说明,前面说的都是一些代码和原理方面的东西.接下来说的会有界面和代码结合,会有成就感,由于能真正的做出东西来了. Windows移动开发包含Windows ...

  6. Android给定坐标计算距离

    给定两点的经纬度.计算两点之间的距离.这里要注意经纬度一定要依照顺序填写 1. 利用android中的工具获得,单位是米 float[] results=new float[1]; Location. ...

  7. 菜鸟nginx源代码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)

    菜鸟nginx源代码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...

  8. lucene 范围过滤

    Lucene里面有关于Filter的整体知识 下面,我们来看下具体的在代码里怎么实现,先来看下我们的测试数据 Java代码    id      score       bookname    ena ...

  9. easyui+struts2:datagrid无法不能得到数据

    转自:https://bbs.csdn.net/topics/390980437 easyui+struts2:datagrid无法访问到指定action: userlist.jsp部分代码: XML ...

  10. 77.招聘信息管理 EXTJS 页面

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...