标签:des style blog class code c
Trucking
Time Limit: 20000/10000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 1692 Accepted
Submission(s): 587
Problem Description
A certain local trucking company would like to
transport some goods on a cargo truck from one place to another. It is desirable
to transport as much goods as possible each trip. Unfortunately, one cannot
always use the roads in the shortest route: some roads may have obstacles (e.g.
bridge overpass, tunnels) which limit heights of the goods transported.
Therefore, the company would like to transport as much as possible each trip,
and then choose the shortest route that can be used to transport that
amount.
For the given cargo truck, maximizing the height of the goods
transported is equivalent to maximizing the amount of goods transported. For
safety reasons, there is a certain height limit for the cargo truck which cannot
be exceeded.
Input
The input consists of a number of cases. Each case
starts with two integers, separated by a space, on a line. These two integers
are the number of cities (C) and the number of roads (R). There are at most 1000
cities, numbered from 1. This is followed by R lines each containing the city
numbers of the cities connected by that road, the maximum height allowed on that
road, and the length of that road. The maximum height for each road is a
positive integer, except that a height of -1 indicates that there is no height
limit on that road. The length of each road is a positive integer at most 1000.
Every road can be travelled in both directions, and there is at most one road
connecting each distinct pair of cities. Finally, the last line of each case
consists of the start and end city numbers, as well as the height limit (a
positive integer) of the cargo truck. The input terminates when C = R = 0.
Output
For each case, print the case number followed by the
maximum height of the cargo truck allowed and the length of the shortest route.
Use the format as shown in the sample output. If it is not possible to reach the
end city from the start city, print "cannot reach destination" after the case
number. Print a blank line between the output of the cases.
Sample Input
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 10
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 4 3
1 1 2 -1 100
1 3 10
0 0
Sample Output
Case 1:
maximum height = 7
length of shortest route = 20
Case 2:
maximum height = 4
length of shortest route = 8
Case 3:
cannot reach destination
Source
Recommend
gaojie | We have
carefully selected several similar problems for you:
2722 2433 2923 2482 2377
一口血喷在了屏幕上...
题目不难而且我的初始思路是对的,开始觉得数据不大没想用二分求高度而已,而且没用二分时间差不大,一倍多。
最后检查了几次,wa了好几次,才发现是没有跳出 0 0!!还以为二分又写错了= =
用C写的邻接表比vector快点。
最短路+枚举高度:
1 //140MS 600K 1953 B C++
2 #include<iostream>
3 #include<queue>
4 #define N 1005
5 #define inf 0x3fffffff
6 using namespace std;
7 struct node{
8 int v,h,d;
9 int next;
10 }edge[20*N];
11 vector<node>V[N];
12 int d[N];
13 int vis[N];
14 int n,s,e;
15 int head[N],edgenum;
16 void addedge(int u,int v,int h,int d)
17 {
18 edge[edgenum].v=v;
19 edge[edgenum].h=h;
20 edge[edgenum].d=d;
21 edge[edgenum].next=head[u];
22 head[u]=edgenum++;
23 }
24 void spfa(int s,int maxh)
25 {
26 for(int i=0;i<=n;i++)
27 d[i]=inf;
28 memset(vis,0,sizeof(vis));
29 queue<int>Q;
30 d[s]=0;
31 Q.push(s);
32 vis[s]=1;
33 while(!Q.empty()){
34 int u=Q.front();
35 Q.pop();
36 vis[u]=0;
37 for(int i=head[u];i!=-1;i=edge[i].next){
38 int v=edge[i].v;
39 int h=edge[i].h;
40 int w=edge[i].d;
41 if(d[v]>d[u]+w && h>=maxh){
42 d[v]=d[u]+w;
43 if(!vis[v]){
44 Q.push(v);
45 vis[v]=1;
46 }
47 }
48 }
49 }
50 }
51 int main(void)
52 {
53 int m,a,b,h,c,k=1;
54 while(scanf("%d%d",&n,&m)!=EOF && (n+m))
55 {
56 if(k!=1) printf("\n");
57 memset(head,-1,sizeof(head));
58 for(int i=0;i<=n;i++)
59 V[i].clear();
60 edgenum=0;
61 for(int i=0;i<m;i++){
62 scanf("%d%d%d%d",&a,&b,&h,&c);
63 if(h==-1) h=inf;
64 addedge(a,b,h,c);
65 addedge(b,a,h,c);
66 }
67 scanf("%d%d%d",&s,&e,&h);
68 printf("Case %d:\n",k++);
69 int ans=inf;
70 int l=0,r=h;
71 while(l<r){
72 int mid=(l+r+1)>>1;
73 spfa(s,mid);
74 if(d[e]!=inf){
75 l=mid;ans=d[e];
76 }else r=mid-1;
77 }
78 if(ans!=inf){
79 printf("maximum height = %d\n",r);
80 printf("length of shortest route = %d\n",ans);
81 }else printf("cannot reach destination\n");
82 }
83 return 0;
84 }
hdu 2962 Trucking (最短路径),布布扣,bubuko.com
hdu 2962 Trucking (最短路径)
标签:des style blog class code c
原文地址:http://www.cnblogs.com/GO-NO-1/p/3732267.html