zoukankan      html  css  js  c++  java
  • pat1030. Travel Plan (30)

    1030. Travel Plan (30)

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

    A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

    City1 City2 Distance Cost

    where the numbers are all integers no more than 500, and are separated by a space.

    Output Specification:

    For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

    Sample Input
    4 5 0 3
    0 1 1 20
    1 3 2 30
    0 3 4 10
    0 2 2 20
    2 3 1 20
    
    Sample Output
    0 2 3 3 40
    

    提交代码

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<queue>
      6 #include<vector>
      7 #include<stack>
      8 using namespace std;
      9 #define inf 260000 
     10 int dist[505],cost[505];
     11 struct node{
     12     int v,dist,cost;
     13     node *next;
     14     node(){
     15         next=NULL;
     16     }
     17 };
     18 int main(){
     19     //freopen("D:\input.txt","r",stdin);
     20     int n,m,s,d;
     21     scanf("%d %d %d %d",&n,&m,&s,&d);
     22     node *h=new node[n];
     23     int *fr=new int[n];
     24     int i,a,b,dis,c;
     25     node *p;
     26     
     27     //cout<<n<<endl;
     28     
     29     for(i=0;i<m;i++){
     30         scanf("%d %d %d %d",&a,&b,&dis,&c);
     31         p=new node();
     32         p->v=b;
     33         p->dist=dis;
     34         p->cost=c;
     35         p->next=h[a].next;
     36         h[a].next=p;
     37         
     38         p=new node();
     39         p->v=a;
     40         p->dist=dis;
     41         p->cost=c;
     42         p->next=h[b].next;
     43         h[b].next=p;
     44     }
     45     
     46     for(i=0;i<n;i++){
     47         dist[i]=cost[i]=inf;
     48         fr[i]=i;
     49     }
     50     //memset(dist,-1,sizeof(dist));
     51     //memset(cost,-1,sizeof(cost));
     52     dist[s]=cost[s]=0;
     53     //int totaldist=0,totalcost=0;
     54     
     55     //cout<<n<<endl;
     56     
     57     int e=s;
     58     while(e!=d){
     59         p=h[e].next;
     60         while(p){
     61             if(dist[p->v]>dist[e]+p->dist){
     62                 dist[p->v]=dist[e]+p->dist;
     63                 cost[p->v]=cost[e]+p->cost;
     64                 fr[p->v]=e;
     65             }
     66             else{
     67                 if(dist[p->v]==dist[e]+p->dist&&cost[p->v]>cost[e]+p->cost){
     68                     cost[p->v]=cost[e]+p->cost;
     69                     fr[p->v]=e;
     70                 }
     71             }
     72             p=p->next;
     73         }
     74         dist[e]=0;
     75         
     76         int mindist=inf,mincost=inf;
     77         for(i=0;i<n;i++){
     78                 if(dist[i]&&dist[i]<mindist){
     79                     mindist=dist[i];
     80                     mincost=cost[i];
     81                     e=i;
     82                 }
     83                 else{
     84                     if(dist[i]&&dist[i]==mindist&&cost[i]<mincost){
     85                         mincost=cost[i];
     86                         e=i;
     87                     }
     88                 }
     89         }
     90     }
     91     stack<int> ss;
     92     int fro=d;
     93     while(fr[fro]!=fro){
     94         ss.push(fro);
     95         fro=fr[fro];
     96     }
     97     ss.push(fro);
     98     printf("%d",ss.top());
     99     ss.pop();
    100     while(!ss.empty()){
    101         printf(" %d",ss.top());
    102         ss.pop();
    103     }
    104     printf(" %d %d
    ",dist[d],cost[d]);
    105     delete []fr;
    106     delete []h;
    107     return 0;
    108 }
  • 相关阅读:
    java.lang.OutOfMemoryError: Java heap space错误及处理办法
    JQuery 引发两次$(document.ready)事件
    用Jquery动态append方式加入标签时Css样式丢失的解决方法
    操作系统第6次实验报告:使用信号量解决进程互斥访问
    操作系统实验五:文件系统
    操作系统第4次实验报告:文件系统
    操作系统第三次实验报告:管道
    操作系统第2次实验报告:创建进程
    OS第1次实验报告:熟悉使用Linux命令和剖析ps命令
    第四次实验报告:使用Packet tracer理解RIP路由协议
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4767946.html
Copyright © 2011-2022 走看看