PAT 甲级 1111 Online Map (30 分) 二刷 (迪杰斯特拉+dfs+格式输出)

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≤N≤500), 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

不得不佩服这道题,真是醉了,一刷一遍过,二刷提交了若干次,全错,但是不知道错误在哪,因为思路和第一次刷的时候一模一样。后来终于改正确了,错误点发生在,格式化输出,能复制粘贴,就不要手打了,他题目的输出的冒号是中文格式 ??

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
    int V2;
    int length;
    int time;
    node(int a,int b,int c)
    {
        V2=a;
        length=b;
        time=c;
    }
    node(){}
};
vector<node> v[505];
int N,M;
int src,des;
int least_len,least_time;
int r1=0x7fffffff;
int r2=0x7fffffff;
vector<int> res1;
vector<int> res2;
bool visit[505]={false};
void dijkstra_shortest();
void dfs_shortest(int curr,int len,int time,vector<int> &t);
void dfs_fastest(int curr,int time,vector<int> &t);
void dijkstra_fastest();
int main()
{
    cin>>N>>M;
    int i,j;
    for(i=0;i<M;i++)
    {
        int a,b,c,d,e;
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
        v[a].push_back( node(b,d,e) );
        if(c==0)
            v[b].push_back( node(a,d,e) );
    }
    cin>>src>>des;
    dijkstra_shortest();
    dijkstra_fastest();

    vector<int> t;
    fill(visit,visit+505,false);
    visit[src]=true;
    dfs_shortest(src,0,0,t);

    t.clear();
    fill(visit,visit+505,false);
    visit[src]=true;
    dfs_fastest(src,0,t);

    if(res1==res2)
    {
        printf("Distance = %d; Time = %d: %d",least_len,least_time,src);
        for(int i=0;i<res1.size();i++)
            printf(" -> %d",res1[i]);
        printf("\n");
    }
    else{
        printf("Distance = %d: %d",least_len,src);
        for(int i=0;i<res1.size();i++)
            printf(" -> %d",res1[i]);
        printf("\n");
        printf("Time = %d: %d",least_time,src);
        for(int i=0;i<res2.size();i++)
            printf(" -> %d",res2[i]);
        printf("\n");
    }
    return 0;
}
void dijkstra_shortest()
{
    bool S[505]={false};
    int dist[505];
    fill(dist,dist+505,0x7fffffff);
    for(int i=0;i<v[src].size();i++)
        dist[ v[src][i].V2 ] = v[src][i].length;
    S[src]=true;
    while(true)
    {
        int minn=0x7fffffff,index=-1;
        for(int i=0;i<505;i++)
            if(!S[i]&&dist[i]<minn)
            {
                minn=dist[i];
                index=i;
            }
        if(index==-1)
            break;
        S[index]=true;
        for(int i=0;i<v[index].size();i++)
            if(!S[ v[index][i].V2 ] && dist[index] + v[index][i].length < dist[ v[index][i].V2 ] )
                dist[ v[index][i].V2 ] = dist[index] + v[index][i].length;
    }
    least_len = dist[des];
}
void dijkstra_fastest()
{
    bool S[505]={false};
    int dist[505];
    fill(dist,dist+505,0x7fffffff);
    for(int i=0;i<v[src].size();i++)
        dist[ v[src][i].V2 ] = v[src][i].time;
    S[src]=true;
    while(true)
    {
        int minn=0x7fffffff,index=-1;
        for(int i=0;i<505;i++)
            if(!S[i]&&dist[i]<minn)
            {
                minn=dist[i];
                index=i;
            }
        if(index==-1)
            break;
        S[index]=true;
        for(int i=0;i<v[index].size();i++)
            if(!S[ v[index][i].V2 ] && dist[index] + v[index][i].time < dist[ v[index][i].V2 ] )
                dist[ v[index][i].V2 ] = dist[index] + v[index][i].time;
    }
    least_time = dist[des];
}
void dfs_shortest(int curr,int len,int time,vector<int> &t)
{
    if(curr==des&&len==least_len)
    {
        if(time<r1)
        {
           res1=t;
           r1=time;
        }
        return;
    }
    if(len>=least_len)
        return;
    for(int i=0;i<v[curr].size();i++)
    {
        if(!visit[ v[curr][i].V2 ])
        {
            t.push_back( v[curr][i].V2 );
            visit[ v[curr][i].V2 ] = true;
            dfs_shortest( v[curr][i].V2, len+v[curr][i].length,time+v[curr][i].time, t );
            t.pop_back();
            visit[ v[curr][i].V2 ] = false;
        }
    }
}
void dfs_fastest(int curr,int time,vector<int> &t)
{
    if(curr==des&&time==least_time)
    {
        if(t.size()<r2)
        {
            res2=t;
            r2=t.size();
        }
        return;
    }
    if(time>=least_time)
        return;
    for(int i=0;i<v[curr].size();i++)
    {
        if(!visit[ v[curr][i].V2 ])
        {
            t.push_back( v[curr][i].V2 );
            visit[ v[curr][i].V2 ] = true;
            dfs_fastest( v[curr][i].V2, time+v[curr][i].time, t );
            t.pop_back();
            visit[ v[curr][i].V2 ] = false;
        }
    }
}

发布了174 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41173604/article/details/100512589