2013-12-10 08:38:49Morris
[UVA][floyd] 10436 - Cheapest way
Brightness of Brain
Contest Problem H Time limit: 1 second Memory: 32 MB |
The Problem
``Moutushi Enterprise'' a leading transport company of Bangladesh is quite anxious about their current business status. For some recent months they aren't earning profitable money as they did before. They investigated over it and found that it is the change in the tax policy that caused them so. For this reason they want to reroute their ways and charge the passengers on the cost of that route.The cost a bus has to bear is the cost of the oil it needs for that journey, along with some money paid on each station it touches. The profit is ten percent of the cost. Then the amount is divided by the total number of sits in the bus and the seat fare is then determined.
You are given the description of the city map, you are to determine the cheapest
way of the map and the seat fare each passenger have to bear.
The Input
The first line of the input indicates the number of map to process. Each map contains three sections. Section A contains the stations that the map contains. First line of this section is an integer ``nStation'' (0<`nStation'<20) indicating number of stations the path have. Next `nStation' line contains station name and the money that have to pay to pass through this station. Next section starts with an integer ``nPath'' (0<`nPath'<20)indicating number of paths that connects two stations. Next `nPath' contains ``station1 station2 distance'', describing `station1' has a path to `station2' with distance equal to `distance' in kilometer and vice versa. Each kilometer costs an oil consumption of 2 taka (unit of money in Bangladesh). The third section consists of a integer ``nQuery'' (0<`nQuery'<10)with `nQuery' line of input. Each line have a query to follow in the format ``station1 station2 passenger'', asking to find the best path from station1 to station2 and what will be the cost for each passenger for a bus with `passenger' amount of seat. It is guaranteed that each map description is valid, and the query has a valid and unique path.The Output
For each map print ``Map #X'', where `X' is the map number starting from 1. Then for each query print ``Query #Y'', where `Y' is the query number for each map starting from 1. Then for each query print the suitable path followed by the fare each passenger have to bear (rounded to the second digit after the decimal point) as shown below.Sample Input
2 4 mirpur12 5 farmgate 8 gulistan 10 newmarket 5 4 mirpur12 farmgate 12 mirpur12 newmarket 20 farmgate gulistan 10 newmarket gulistan 8 2 mirpur12 gulistan 30 mirpur12 newmarket 30 3 uttara 2 farmgate 8 gulistan 10 2 uttara farmgate 35 farmgate gulistan 10 1 uttara gulistan 30
Sample Output
Map #1 Query #1 mirpur12 farmgate gulistan Each passenger has to pay : 2.46 taka Query #2 mirpur12 newmarket Each passenger has to pay : 1.83 taka Map #2 Query #1 uttara farmgate gulistan Each passenger has to pay : 4.03 taka
Suman Mahbub
Created: 11-10-2002
Updated: 14-12-2002, 20-01-2003
題目描述:
每經過一個點就會收費,類似收費亭的概念,每條路都是雙向的,每一公里消耗 2 單位。
最後將成本乘上 1.1,將這些費用交給使用的所有乘客去平攤。
題目解法:
floyd-warshall 解法,由於每過一個點就會收費,直接對每條邊的權重多上端點權重。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int g[105][105], next[105][105];
int main() {
int testcase, cases = 0;
int i, j, k, n, m;
int x, y, v;
char s[105];
char sname[105][105];
int a[105];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
map<string, int> R;
for(i = 0; i < n; i++) {
scanf("%s %d", sname[i], &a[i]);
R[sname[i]] = i;
}
scanf("%d", &m);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++)
g[i][j] = 0xfffffff, next[i][j] = j;
g[i][i] = 0;
}
for(i = 0; i < m; i++) {
scanf("%s", s);
x = R[s];
scanf("%s", s);
y = R[s];
scanf("%d", &v);
v *= 2;
g[x][y] = min(g[x][y], v);
g[y][x] = min(g[y][x], v);
}
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
g[i][j] += a[j];
}
g[i][i] = 0;
}
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
if(g[i][j] > g[i][k] + g[k][j]) {
g[i][j] = g[i][k] + g[k][j];
next[i][j] = next[i][k];
}
scanf("%d", &m);
printf("Map #%d\n", ++cases);
int qcases = 0;
while(m--) {
scanf("%s", s);
x = R[s];
scanf("%s", s);
y = R[s];
scanf("%d", &v);
printf("Query #%d\n", ++qcases);
double pay = (g[x][y] + a[x]) * 1.1 / v;
printf("%s", sname[x]);
x = next[x][y];
while(x != y) {
printf(" %s", sname[x]);
x = next[x][y];
}
printf(" %s", sname[x]);
puts("");
printf("Each passenger has to pay : %.2lf taka\n", pay);
}
if(testcase)
puts("");
}
return 0;
}