PAT-1111 Online Map (30分) 最短路+dfs

明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好

1.没有读清题目要求,或者说没有读完题目,明天一定要注意

2.vis初始化的时候从1初始化到n,应该从0开始,以后初始化就从0到n即可

题目大意:给一张地图,两个结点中既有距离也有时间,有的单行有的双向,要求根据地图推荐两条路线:一条是最快到达路线,一条是最短距离的路线。
第一行给出两个整数N和M,表示地图中地点的个数和路径的条数。接下来的M行每一行给出:道路结点编号V1 道路结点编号V2 是否单行线 道路长度 所需时间
要求第一行输出最快到达时间Time和路径,第二行输出最短距离Distance和路径

就用两次dij+dfs即可,好嘛,今晚dfs也不用练了.最好明天出一道这道题

#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std;

const int maxn=500+5;
const int inf=0x3f3f3f3f;

int T[maxn][maxn];
int dis1[maxn];
int dis2[maxn];
bool vis[maxn];
int n,m,from,to;
vector<int>pre1[maxn],pre2[maxn];
/// vis =0 从0开始
struct edge
{
    int v;
    int length;
    int time;
    edge(int v,int length,int time):v(v),length(length),time(time){}
};
vector<edge>G[maxn];
struct node
{
    int v;
    int len;
    node(int v=0,int len=0):v(v),len(len){}
    bool operator<(const node&r)const
    {
        return len>r.len;
    }

};
void dijkstra1(int start,int n)
{
    for(int i=0;i<=n;i++)
    {
        vis[i]=false;
        dis1[i]=inf;
    }
    dis1[start]=0;
    priority_queue<node>Q;
    Q.push(node(start,0));
    node temp;
    while(!Q.empty())
    {
        temp=Q.top();
        Q.pop();
        int u=temp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i].v;
            int len=G[u][i].length;
            if(!vis[v]&&dis1[v]>dis1[u]+len)
            {
                dis1[v]=dis1[u]+len;
                pre1[v].clear();
                pre1[v].push_back(u);
                Q.push(node(v,dis1[v]));
            }
            else if(!vis[v]&&dis1[v]==dis1[u]+len)
            {
                pre1[v].push_back(u);
            }
        }
    }
}
void dijkstra2(int start,int n)
{
    for(int i=0;i<=n;i++)
    {
        vis[i]=false;
        dis2[i]=inf;
    }
    dis2[start]=0;
    priority_queue<node>Q;
    while(!Q.empty())Q.pop();
    Q.push(node(start,0));

    node temp;
    while(!Q.empty())
    {
        temp=Q.top();
        Q.pop();
        int u=temp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i].v;
            int len=G[u][i].time;
            if(!vis[v]&&dis2[v]>dis2[u]+len)
            {
                dis2[v]=dis2[u]+len;
                pre2[v].clear();
                pre2[v].push_back(u);
                Q.push(node(v,dis2[v]));
            }
            else if(!vis[v]&&dis2[v]==dis2[u]+len)
            {
                pre2[v].push_back(u);
            }
        }
    }
}
vector<int>path;
vector<int>ans_path;
vector<int>ans_path2;
int min_time=inf;
void dfs1(int v)
{
    path.push_back(v);
    if(v==from)
    {
        int temp=0;
        for(int i=0;i<path.size()-1;i++)
        {
            temp+=T[path[i+1]][path[i]];
        }
        if(temp<min_time)
        {
            min_time=temp;///忘记更新最小值了\吐血
            ans_path=path;
        }
        path.pop_back();
        return ;
    }
    for(int i=0;i<pre1[v].size();i++)
    {
        dfs1(pre1[v][i]);
    }
    path.pop_back();

}
int max_n=maxn;
void dfs2(int v)
{
    path.push_back(v);
    if(v==from)
    {
        if(path.size()<max_n)
        {
            max_n=path.size();
            ans_path2=path;
        }
        path.pop_back();
        return;
    }
    for(int i=0;i<pre2[v].size();i++)
    {
        dfs2(pre2[v][i]);
    }
    path.pop_back();
}

/*
V1 V2 one-way length time
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
*/
int main()
{
    cin>>n>>m;
    int v1,v2,flag,len,time;
    each(i,1,m)
    {
        cin>>v1>>v2>>flag>>len>>time;
        if(flag==1)
        {
            G[v1].push_back(edge(v2,len,time));
            T[v1][v2]=T[v2][v1]=time;
        }
        else
        {
            G[v1].push_back(edge(v2,len,time));
            G[v2].push_back(edge(v1,len,time));
            T[v1][v2]=T[v2][v1]=time;
        }
    }
    cin>>from>>to;
    dijkstra1(from,n);
    dijkstra2(from,n);
    //de(dis1[to]);
    //de(dis2[to]);
    dfs1(to);
    dfs2(to);
    printf("Distance = %d", dis1[to]);
    if(ans_path == ans_path2) {
        printf("; Time = %d: ", dis2[to]);
    } else {
        printf(": ");
        for(int i = ans_path.size() - 1; i >= 0; i--) {
            printf("%d", ans_path[i]);
            if(i != 0) printf(" -> ");
        }
        printf("\nTime = %d: ", dis2[to]);
    }
    for(int i = ans_path2.size() - 1; i >= 0; i--) {
        printf("%d", ans_path2[i]);
        if(i != 0) printf(" -> ");
    }
    /*
    Distance = 6: 3 -> 4 -> 8 -> 5
    Time = 3: 3 -> 1 -> 5
    */
}

原文地址:https://www.cnblogs.com/Tony100K/p/11997674.html

时间: 2024-08-02 02:37:27

PAT-1111 Online Map (30分) 最短路+dfs的相关文章

1131 Subway Map (30 分)

1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given th

PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs case 7 过不了求助!!!)

1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Pub

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). No

PAT (Advanced Level) 1111. Online Map (30)

预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,m; int st,en

PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程和最短时间路径一样,合并输出一次即可. 纯粹就是练习dijkstra,没什么难的. 第一次dijkstra求最短路程,记录下每个节点的路程和时间. 第二次dijkstra求最短时间,记录下每个节点的时间和经过的节点数. pre数组用来存储前驱节点,保存路径 #include <iostream>

1111. Online Map (30)

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. Inpu

PAT Advanced 1147 Heaps (30分)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min h

1111 Online Map(迪杰斯特拉+DFS)

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805358663417856 输出要求: 翻译结果: 在最短路径不唯一的情况下,输出最短路径中最快的一条,保证唯一. 如果最快的路径不是唯一的,则输出通过最少交叉口的路径,该交叉口保证是唯一的.如果最短路径和最快路径相同,请按以下格式将它们打印在一行中: 题目思路很简答,使用两个不同度量(距离.时间)的 迪杰斯特拉+DFS 求解即可. PS:我英语好菜,我翻译错了,

PAT甲级——1131 Subway Map (30 分)

可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing