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 ...
随机推荐
- 基于k8s的ES集群定期删除索引
apiVersion: batch/v1beta1 kind: CronJob metadata: name: elasticsearch namespace: elasticsearch label ...
- March 12 2017 Week 11 Sunday
I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. I know the value of hard work, ...
- Tomcat与MySQL的数据源连接方法
Tomcat配置数据源,由于项目经常访问数据库,需要不断地打开关闭,这就耗费了大量的资源.所以用数据源的方式访问数据库. 大体步骤: 配置server.xml 配置项目所在的WebRoot/WEB-I ...
- 郑州集训day1自闭有感
被拉到郑州培训了 考了一上午莫名自闭 帮助慎老师拿到\(rk1\)非常开心 简述一下题目吧 T1.まんふは函数 原题地址 考原题还行 据说是\(Huffman\)树 在成爷爷的再三讲解下,我终于明白了 ...
- 【JeeSite】登录和主题切换
最高管理员账号,用户名:thinkgem 密码:admin 1. 密码加密:登录用户密码进行SHA1散列加密,此加密方法是不可逆的.保证密文泄露后的安全问题. 在spring-shiro配置文件 ...
- OpenMP使用体验报告(概述)
(本文原创,首次使用OpenMP,将使用体会记录下来供学习) OpenMP是啥玩意??? 多核多线程处理器的出现,让并行计算成为可能.在此之前,单核处理器并不能并行计算,这是很显然的,只有一个核心只能 ...
- android 学习笔记 杂记1
getIntent().getExtras().get("intent"); 这个intent是数据包装的参数. 比如: Intent intent = new Intent(th ...
- 兼容性良好的 sticky-footer 布局
<div class="content"> <div class="content-wrapper"> <div class=&q ...
- File zilla远程连接服务器报错:服务器发回了不可路由的地址,使用服务器地址代替
百度的答案都是:更改Filezilla设置,编辑-设置-连接-FTP-被动模式,将“使用服务器的外部ip地址来代替”改为“回到主动模式”即可.但问题没有解决!!! 由于使用的是阿里云的服务器.安全组里 ...
- 你不知道的javaScript笔记(3)
对象 对象可以通过两种形式定义: 声明形式和构造形式 声明形式语法: var myObj = {key:value} 构造形式语法: var myObj = new Object(); myObj.k ...