zoukankan      html  css  js  c++  java
  • UVA 11374 Airport Express

    虽然这个题写的很挫。但还是水一下。

    View Code
      1 #include <iostream>
      2 #include <vector>
      3 #include <queue>
      4 #include <cstring>
      5 #include <cstdio>
      6 #define INF 1 << 20
      7 using namespace std;
      8 typedef pair<int,int> pii;
      9 int N,S,E,M,K;
     10 int G[505][505],f[505],g[505],T[1010][3];
     11 int path1[1000];//从起点s到a的逆序路径
     12 int path2[1000];//从b到终点e的顺序路径
     13 
     14 void Read_Graph()
     15 {
     16     memset(path1,0,sizeof(path1));
     17     memset(path2,0,sizeof(path2));
     18     for(int i = 0;i <= N;i++)
     19         for(int j = 0;j <= N;j++)
     20             G[i][j] = INF;
     21     int i,s,e,l;
     22     scanf("%d",&M);
     23     for(i = 1;i <= M;i++)
     24     {
     25         scanf("%d%d%d",&s,&e,&l);
     26         G[s][e] = G[e][s] = l;
     27     }
     28     scanf("%d",&K);
     29     for(i = 1;i <= K;i++)
     30         scanf("%d%d%d",&T[i][0],&T[i][1],&T[i][2]);
     31 }
     32 
     33 void Dijkstra(int start,int d[])
     34 {
     35     priority_queue< pii,vector<pii>,greater<pii> > q;
     36 
     37     int ith,i;
     38 
     39     for(i = 1;i <= N;i++)   d[i] = INF;
     40     d[start] = 0;
     41     q.push(make_pair(0,start));
     42 
     43     while(!q.empty())
     44     {
     45         while(!q.empty() && q.top().first > d[q.top().second])
     46         {
     47             q.pop();
     48         }
     49         if(q.empty())   break;
     50         ith = q.top().second;
     51         q.pop();
     52         for(i = 1;i <= N;i++)
     53         {
     54             if(d[i] > d[ith] + G[ith][i])
     55             {
     56                 d[i] = d[ith] + G[ith][i];
     57                 q.push(make_pair(d[i],i));
     58             }
     59         }
     60     }
     61 }
     62 
     63 void solve()
     64 {
     65     int i,a,b,t,temp1,temp2,dis,pos,count1,count2;
     66     Dijkstra(S,f);
     67     Dijkstra(E,g);
     68     int ans = f[E];
     69     a = 0;
     70     //查询每一条商业线
     71     for(i = 1;i <= K;i++)
     72     {
     73         temp1 = T[i][0];
     74         temp2 = T[i][1];
     75         t = T[i][2];
     76 
     77         if(f[temp1] + t + g[temp2] < ans)
     78         {
     79             ans = f[temp1] + t + g[temp2];
     80             a = temp1;
     81             b = temp2;
     82         }
     83 
     84         if(f[temp2] + t + g[temp1] < ans)
     85         {
     86             ans = f[temp2] + t + g[temp1];
     87             a = temp2;
     88             b = temp1;
     89         }
     90     }
     91 
     92     if(a)//如果用掉了商业线的票
     93     {
     94         dis = f[a];
     95         pos = a;
     96         count1 = 0;
     97         path1[count1] = pos;
     98         while(dis != 0)
     99         {
    100             for(i = 1;i <= N;i++)
    101             {
    102                 if(f[i] + G[i][pos] == f[pos])
    103                 {
    104                     pos = i;
    105                     dis = f[i];
    106                     path1[++count1] = pos;
    107                     break;
    108                 }
    109             }
    110         }
    111 
    112         dis = g[b];
    113         pos = b;
    114         count2 = 0;
    115         path2[count2] = pos;
    116         while(dis != 0)
    117         {
    118             for(i = 1;i <= N;i++)
    119             {
    120                 if(g[i] + G[i][pos] == g[pos])
    121                 {
    122                     pos = i;
    123                     dis = g[i];
    124                     path2[++count2] = pos;
    125                     break;
    126                 }
    127             }
    128         }
    129         printf("%d",path1[count1]);
    130         for(i = count1 - 1;i >= 0;i--)
    131             printf(" %d",path1[i]);
    132         for(i = 0;i <= count2;i++)
    133             printf(" %d",path2[i]);
    134         printf("\n");
    135         printf("%d\n",a);
    136     }
    137 
    138     else
    139     {
    140         dis = f[E];
    141         pos = E;
    142         count1 = 0;
    143         path1[count1] = pos;
    144         while(dis != 0)
    145         {
    146             for(i = 1;i <= N;i++)
    147             {
    148                 if(f[i] + G[i][pos] == f[pos])
    149                 {
    150                     pos = i;
    151                     dis = f[i];
    152                     path1[++count1] = pos;
    153                     break;
    154                 }
    155             }
    156         }
    157         printf("%d",path1[count1]);
    158         for(i = count1 - 1;i >= 0;i--)
    159             printf(" %d",path1[i]);
    160         printf("\n");
    161         printf("Ticket Not Used\n");
    162     }
    163     printf("%d\n",ans);
    164 
    165 }
    166 int main()
    167 {
    168     bool first = true;
    169     while(scanf("%d%d%d",&N,&S,&E) == 3)
    170     {
    171         if(first)   first = false;
    172         else    printf("\n");
    173         Read_Graph();
    174         solve();
    175     }
    176     return 0;
    177 }
  • 相关阅读:
    C盘格式化
    电脑显示器有波纹抖动怎么办
    磁盘碎片
    如何把Excel另存为XML格式文件(快速转换)
    题目1551:切蛋糕
    题目1552:座位问题
    题目1550:分糖果
    题目1493:公约数
    题目1544:数字序列区间最小值
    RMQ (Range Minimum/Maximum Query)算法
  • 原文地址:https://www.cnblogs.com/zhexipinnong/p/2818569.html
Copyright © 2011-2022 走看看