PAT 1111. Online Map (30) Dijkstra

1111. Online Map (30)

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

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
 
 
没坑点。和前面的图题差不多,只是把两趟dijkstra合成一趟。注意数据量n=500,别用floyid就行
用邻接矩阵时间复杂度为O(n*(4n))完全符合。想再提速改成邻接表就行了。
 
 
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<deque>
using namespace std;

int road[501][501];  //路
int dist[501];      //np1最短距离
int fast[501];      //np2最快时间
int ttime[501][501];  //np1到达某节点的时间
int n,m;
int last1[501];   //记录最短的(np1)父节点
int last2[502];   //记录最快的(np2)父节点
int path1[501];   // np1的路径
int path2[501];   // np2的路径
int p1,p2;

void dijkstra(int start,int endd)
 {
     int np1,np2;
     np1=np2=start;
     int mark1[501]={0};
     int mark2[501]={0};
     int timest[501]={0};   // np1的时间记录
     int howmany[501]={0};  // np2的节点记录
     memset(timest,-1,sizeof(timest));
     memset(dist,-1,sizeof(dist));
     //memset(fast,-1,sizeof(fast));
     for(int i=0;i<501;i++)
     {
         timest[i]=howmany[i]=fast[i]=999999;
     }
     fast[start]=dist[start]=0;
     timest[np1]=0;
     howmany[np2]=0;
     mark1[start]=mark2[start]=1;

     for(int i=1;i<n;i++)
     {
         for(int j=0;j<n;j++)
         {
             if(mark1[j]||road[np1][j]==-1) continue;
             if(dist[j]==-1||dist[j]>dist[np1]+road[np1][j])
              {
                  dist[j]=dist[np1]+road[np1][j];
                  timest[j]=timest[np1]+ttime[np1][j];
                  last1[j]=np1;
              }
             else if(dist[j]==dist[np1]+road[np1][j]&×t[j]>timest[np1]+ttime[np1][j])
             {
                 timest[j]=timest[np1]+ttime[np1][j];
                 last1[j]=np1;
             }
         }
         for(int j=0;j<n;j++)
         {
             if(mark2[j]||road[np2][j]==-1) continue;
             if(fast[j]>fast[np2]+ttime[np2][j])
             {
                 fast[j]=fast[np2]+ttime[np2][j];
                 howmany[j]=howmany[np2]+1;
                 last2[j]=np2;
             }
             else if(fast[j]==fast[np2]+ttime[np2][j]&&howmany[j]>howmany[np2]+1)
                {
                    howmany[j]=howmany[np2]+1;
                    last2[j]=np2;
                }
         }
             int minn1=0x7fffffff;
             int minn2=999990;
             np1=np2=start;
            for(int i=0;i<n;i++)
            {
                if(dist[i]==-1||mark1[i]) continue;
                if(dist[i]<minn1)
                {
                    np1=i;
                    minn1=dist[i];
                }
            }
             for(int i=0;i<n;i++)
             {
                 if(mark2[i]||fast[i]==999999) continue;
                 if(fast[i]<minn2)
                 {
                     np2=i;
                     minn2=fast[i];
                 }
             }

             mark1[np1]=mark2[np2]=1;
           //  cout<<np1<<' '<<dist[np1]<<' '<<np2<<' '<<fast[np2]<<endl;
     }
 }

int main()
{
    cin>>n>>m;
    memset(road,-1,sizeof(road));
    for(int i=0;i<m;i++)
    {
        int x,y,z,a,s;
        cin>>x>>y>>z>>a>>s;
        if(z==0)
        {
           road[x][y]=road[y][x]=a;
           ttime[x][y]=ttime[y][x]=s;
        }
        else
        {
           road[x][y]=a;
           ttime[x][y]=s;
        }
    }
      int start,endd;
      cin>>start>>endd;
     dijkstra(start,endd);
     p1=p2=0;
     int tmp=endd;
     while(tmp!=start)
     {
         path1[p1++]=tmp;
         tmp=last1[tmp];
     }
       path1[p1++]=start;

      tmp=endd;
     while(tmp!=start)
     {
         path2[p2++]=tmp;
         tmp=last2[tmp];
     }
       path2[p2++]=start;

     int flag;
     if(p1!=p2) flag=1;
     else
     {
         flag=0;
         for(int i=0;i<p1;i++)
          if(path1[i]!=path2[i]) flag=1;
     }

     if(flag==0)
     {

         cout<<"Distance = "<<dist[endd];
         cout<<"; ";
         cout<<"Time = "<<fast[endd]<<": ";
         cout<<start;
         for(int i=p1-2;i>=0;i--)
          cout<<" -> "<<path1[i];
     }
     else
     {
         cout<<"Distance = "<<dist[endd];
         cout<<": ";
         cout<<start;
         for(int i=p1-2;i>=0;i--)
          cout<<" -> "<<path1[i];
          cout<<endl;
         cout<<"Time = "<<fast[endd]<<": ";
         cout<<start;
         for(int i=p2-2;i>=0;i--)
          cout<<" -> "<<path2[i];
     }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值