PAT 1111. Online Map (30) Dijkstra

来源:互联网 发布:python写gui应用程序 编辑:程序博客网 时间:2024/05/22 09:05

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 150 1 0 1 18 0 0 1 14 8 1 1 13 4 0 3 23 9 1 4 10 6 0 1 17 5 1 2 18 5 1 2 12 3 0 2 22 1 1 1 11 3 0 3 11 4 0 1 19 7 1 3 15 1 0 5 26 5 1 1 23 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 90 4 1 1 11 6 1 1 32 6 1 1 12 5 1 2 23 0 0 1 13 1 1 1 33 2 1 1 24 5 0 2 26 5 1 1 23 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;}


原创粉丝点击