POJ1529 Shipping Routes

                                                                                    Shipping Routes
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 309   Accepted: 171

Description

The Slow Boat to China Shipping company needs a program to help them quickly quote costs to prospective customers. The cost of a shipment depends on the size of the shipment and on how many shipping legs it requires. A shipping leg connects two warehouses, but since every pair of warehouses is not directly connected by a leg, it might require more than one leg to send a shipment from one warehouse to another.
A data set can represent from 1 to 30 warehouses. A two-letter code name will identify each warehouse (capital letters only). Shipping legs can exist between any two distinct warehouses. All legs are bidirectional.

The cost of a shipment is equal to the size of the shipment times the number of shipping legs required times $100.

The input to the program identifies the warehouse code names and the existence of all shipping legs. For a given shipping request, consisting of the size of the shipment, the source warehouse and the destination warehouse, the program will output the best (cheapest) cost for the shipment, if it is possible to send shipments from the requested source to the requested destination. Alternately, the program must state that the request cannot be fulfilled.

Input

The first line will contain an integer from 1 to 10 inclusive that represents the number of data sets in the input file. Each data set represents a new shipping configuration.

The first line of data in a data set will contain three integers, say M, N, and P: M is an integer from 1 to 30 inclusive representing the number of warehouses in the data set; N is an integer from 0 to M*(M-1)/2 inclusive that represents the number of legs between warehouses in the data set; P is an integer from 0 to 10 inclusive that represents the number of shipping requests for which cost information is required.

The second line of data in a data set contains M two-letter code names for the M warehouses of the data sets. Only capital letters are used. A single blank separates code names.

N lines follow the line of code names, containing shipping leg information in the format: ``XX YY", with XX and YY being the codes for two distinct warehouses in the set that have a direct link (a shipping leg) between them. There will be a single blank between the warehouse codes.

The N lines of shipping leg information are followed by P lines of shipping requests, one request per line. Each shipping request will begin with an integer between 1 and 20 inclusive that represents the size of the shipment. The shipment size will be followed by a pair of code names in the format ``AA BB", with AA and BB being the code for two distinct warehouses in the set which represent the source and destination of the requested shipment.

The input will be valid and consistent. A shipping leg will only be represented once within a data set. Data about legs will always refer to warehouses that have been identified as belonging to the data set. See the example below for clarification of the input format.

Output

The first line of output should read ``SHIPPING ROUTES OUTPUT". For each data set there should be a section of output enumerating which data set the output section represents followed by P lines of the required information. Each of the P lines should list either the cheapest cost of the respective shipment or the phrase ``NO SHIPMENT POSSIBLE". The end of the output should be noted also. Produce output consistent with the example below.

Sample Input

2
6 7 5
AA CC QR FF DD AB
AA CC
CC QR
DD CC
AA DD
AA AB
DD QR
AB DD
5 AA AB
14 DD CC
1 CC DD
2 AA FF
13 AB QR
3 0 1
AA BB CC
5 AA CC

Sample Output

SHIPPING ROUTES OUTPUT

DATA SET 1

$500
$1400
$100
NO SHIPMENT POSSIBLE
$2600

DATA SET 2

NO SHIPMENT POSSIBLE

END OF OUTPUT

Source

 
 
思路:本题首先给出一些点,这些点都是以两个大写字母表示。然后会给出直接相连的那些点。这些点之间的距离可以看做是1(而这两点之间所需要的花费为$100),然后接着会给出题目所要求出的一系列问题,每个问题一行,形式如:14 DD CC,表示:点DD到点CC运输数量为14的货物,所需要的最小花费。花费为:运输量*两点之间的最短距离。
        首先利用FLOYD求出个点之间的最短距离,然后判断两点之间是否路径通过。如果没有,就输出 NO SIPMET POSSIBLE,否则输出这两点之间运输相应货物量的最小花费(最小距离*货物量)。
 
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <map>
  8 #define MAXINT 99999999
  9 
 10 using namespace std;
 11 
 12 
 13 char mp[100][3];
 14 int len=1;
 15 
 16 int data[100][100];
 17 
 18 
 19 int findv(char tmpStr[])
 20 {int i;
 21 
 22 for(i=1;i<len;i++)
 23 if(strcmp(tmpStr,mp[i])==0)
 24   return i;
 25   return 0;
 26 }
 27 
 28 
 29 
 30  
 31  
 32  int insertv(char tmpStr[])
 33  {
 34      strcpy(mp[len],tmpStr);
 35      len++;
 36      return len;
 37  }
 38  
 39  
 40  
 41      
 42  
 43 
 44 
 45 int main()
 46 {
 47     int n,m,p;
 48     
 49     int i,j,k;
 50     int q;
 51     
 52     scanf("%d",&q);
 53     getchar();
 54     
 55     
 56     
 57     
 58     int t;
 59     for(t=1;t<=q;t++)
 60     {
 61                      len=1;
 62                      
 63                      scanf("%d%d%d",&n,&m,&p);
 64                      getchar();
 65                      
 66                      for(i=0;i<n;i++)
 67                      {char tmpStr[3];
 68                       scanf("%s",tmpStr);
 69                       getchar();
 70                       
 71                      insertv(tmpStr);
 72                      
 73                      }
 74                      
 75                      for(i=0;i<len;i++)
 76                      for(j=0;j<len;j++)
 77                      data[i][j]=MAXINT;
 78                      
 79                      
 80                      for(i=0;i<m;i++)
 81                      {
 82                                      char s1[10],s2[10];
 83                                      
 84                                      scanf("%s %s",s1,s2);
 85                                      getchar();
 86                                      
 87                                      int v1=findv(s1);
 88                                      int v2=findv(s2);
 89                                      
 90                                      data[v1][v2]=1;
 91                                      data[v2][v1]=1;
 92                      }
 93                      
 94                      
 95                      
 96                      for(k=1;k<=n;k++)
 97                      for(i=1;i<=n;i++)
 98                      for(j=1;j<=n;j++)
 99                      {
100                                       if(data[i][j]>data[i][k]+data[k][j])
101                                       data[i][j]=data[i][k]+data[k][j];
102                      }
103                      if(t==1)
104                      printf("SHIPPING ROUTES OUTPUT\n\n");
105                      
106                      
107                      if(t>1)
108                       printf("\nDATA SET %d\n\n",t);
109                       else
110                       printf("DATA SET %d\n\n",t);
111                      
112                      for(i=0;i<p;i++)
113                      {
114                                      int cost;
115                                      char s1[10],s2[10];
116                                      
117                                      scanf("%d %s %s",&cost,s1,s2);
118                                      getchar();
119                                      
120                                      int v1=findv(s1);
121                                      int v2=findv(s2);
122                                      if(data[v1][v2]<MAXINT)
123                                      {printf("$%d\n",data[v1][v2]*cost*100);}
124                                      else
125                                      printf("NO SHIPMENT POSSIBLE\n");
126                      }
127     }
128                      printf("\nEND OF OUTPUT\n");
129                      
130                                      
131                                      
132                                      
133                       
134                       
135           
136          
137                      
138                      
139                      
140                      
141     
142     
143     
144     
145     
146     //system("PAUSE");
147     
148     return 0;
149 }

 

posted @ 2012-08-06 22:32  cseriscser  阅读(403)  评论(0编辑  收藏  举报