Big Truck
Your boss has hired you to drive a big truck, transporting items between two locations in a city. You’re given a description of the city, with locations of interest and the lengths of roads between them. Your boss requires that you take a shortest path between the starting and ending location, and she’ll check your odometer when you’re done to make sure you didn’t take any unnecessary side trips. However, your friends know you have plenty of unused space in the truck, and they have asked you to stop by several locations in town, to pick up items for them. You’re happy to do this for them. You may not be able to visit every location to pick up everything your friends want, but you’d like to pick up as many items as possible on your trip, as long as it doesn’t make the path any longer than necessary.
The two graphs above show examples of what the city may look like, with nodes representing locations, edges representing roads and dots inside the nodes representing items your friends have asked you to pick up. Driving through a location allows you to pick up all the items there; it’s a big truck, with no limit on the items it can carry. In the graph on the left, for example, you have to drive the big truck from location 11 to location 66. If you follow the path 1→2→3→61→2→3→6, the length is 99, and you’ll get to pick up 44 items. Of course, it would be better to drive 1→4→5→61→4→5→6; that’s still a length of 99, but going this way instead lets you pick up an additional item. Driving1→4→3→61→4→3→6 would let you pick up even more items, but it would make your trip longer, so you can’t go this way.
Input
The first line of input contains an integer, nn (2≤n≤1002≤n≤100), giving the number of locations in the city. Locations are numbered from 11 to nn, with location 11 being the starting location and nn being the destination. The next input line gives a sequence of nn integers, t1…tnt1…tn, where each titi indicates the number of items your friends have asked you to pick up from location ii. All the titi values are between 00 and 100100, inclusive. The next input line contains a non-negative integer, mm, giving the number of roads in the city. Each of the following mm lines is a description of a road, given as three integers, abdabd. This indicates that there is a road of length dd between location aa and location bb. The values of aa and bb are in the range 1…n1…n, and the value of dd is between 11 and 100100, inclusive. All roads can be traversed in either direction, there is at most one road between any two locations, and no road starts and ends at the same location.
Output
If it’s not possible to travel from location 11 to location nn, just output out the word “impossible”. Otherwise, output the length of a shortest path from location 11 to location nn, followed by the maximum number of items you can pick up along the way.
| Sample Input 1 | Sample Output 1 |
|---|---|
6 |
9 5 |
| Sample Input 2 | Sample Output 2 |
|---|---|
9 |
12 7 |
| Sample Input 3 | Sample Output 3 |
|---|---|
2 |
impossible |
题解:
其实就是一个最短路,只不过过程需要记录一下每个点的权值,并保留,
#include <bits/stdc++.h>
using namespace std;
const int MAXN=110;
const int INF=0x3f3f3f3f;
int n;
int val[MAXN];
int mapp[MAXN][MAXN];
int dis[MAXN],ans[MAXN];
bool vis[MAXN];
void dij()
{
for(int i=1;i<=n;i++)
dis[i]=mapp[1][i];
dis[1]=0;
int MAIN,V=-1,k;
for (int i = 1; i <=n ; ++i) {
MAIN=INF;
k=V;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&MAIN>dis[j])
{
MAIN=dis[j];
V=j;
} }
vis[V]=true;
for (int j = 1; j <=n ; ++j) {
if(!vis[j]&&dis[j]>dis[V]+mapp[V][j])
{
dis[j]=dis[V]+ mapp[V][j];
ans[j]=ans[V]+val[j];
} else if(!vis[j]&&dis[j]==dis[V]+mapp[V][j])
{
ans[j]=max(ans[j],ans[V]+val[j]);
}
}
} if(dis[n]==0)
printf("impossible\n");
else
printf("%d %d\n",dis[n],ans[n]+val[1]);
}
int main()
{
scanf("%d",&n);
memset(mapp,0x3f, sizeof(mapp));
for (int i =1; i <=n ; ++i) {
scanf("%d",&val[i]);
mapp[i][i]=0;
}
int m;scanf("%d",&m);
int x,y,z; while(m--)
{
scanf("%d%d%d",&x,&y,&z);
mapp[x][y]=mapp[y][x]=z;
}
dij();
return 0; }
//HDU-3790 /* 9
1 1 1 1 1 1 1 1 1
10
1 2 3
2 5 3
1 6 2
6 7 2
7 5 2
5 3 1
3 4 2
4 9 3
5 8 2
8 9 4 6
1 1 2 3 1 0
7
1 2 2
2 3 2
3 6 4
1 4 4
4 3 2
4 5 3
5 6 2 */
Big Truck的更多相关文章
- Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...
- Truck History(prim & mst)
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19772 Accepted: 7633 De ...
- poj1789 Truck History
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
package car; public class Vehicle { //定义成员变量 private int wheels; private double weight; public int g ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- Truck History(poj 1789)
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- Truck History--poj1789
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21534 Accepted: 8379 De ...
随机推荐
- 深入理解JVM读书笔记思维导图
为了证明我已经啃完这本书然后买新书不用剁手...脑图画了8个钟,感觉整个人都不好了T_T 脑细胞不知道死了多少... 其实没吃透,估计若干年后要重新翻开来看...
- Struts2学习-拦截器
1.新建项目user4,建立好和user3一样的目录,与之相比只是添加几个类,主要是struts.xml和action类的改变,其结果没有太大的变化 struts,xml <?xml versi ...
- Java使用FileReader(file)、readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号。
//Java使用FileReader(file).readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号. public static void readFi ...
- Jmeter入门18 Jmeter添加cookie的两种方式
jmeter中添加cookie可以通过配置HTTP Cookie Manager,也可以通过HTTP Header Manager,因为cookie是放在头文件里发送的. 实例:博客园点击添加新随笔 ...
- Softmax实现 fashion.mnist 分类
softmax #!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'i ...
- Runloop理解
看了一堂公开课,自己小结一下: Runloop: 内部有三个东东:(Source, Timer, Observer) 作用/本质:1.死循环 (为app 保活): 2.监听处理事件 Timer 理解: ...
- unbuntu循环登录
http://www.myexception.cn/operating-system/1707766.html
- springmvc时间(date)无法转入后台(@DateTimeFormat+@JsonFormat(GMT+8))
spring时间(date)无法转入后台 Type Status Report Description The server cannot or will not process the reques ...
- ORA-01950:对表空间 'USERS' 无权限
在存储数据时出现ORA-01950:对表空间 'USERS' 无权限 错误,解决如下: 找到对象Users下的用户名,然后点编辑,角色权限添加dba 在oracle创建了一个新的表空间和一个新的用户, ...
- 课时57.HTML被废弃的标签(掌握)
1.为什么HTML中有一部分标签会被废弃? 因为当前HTML中的标签只有一个作用,就是用来添加语义,而早期的HTML标签中有一部分标签是没有语义的 有一部分标签是用来修改样式的 所以这部分标签就被淘汰 ...