Photo by Phil Whitehouse

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.

Figure 1: Illustrations of the first two sample inputs

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
1 1 2 3 1 0
7
1 2 2
2 3 3
3 6 4
1 4 4
4 3 2
4 5 3
5 6 2
9 5
Sample Input 2 Sample Output 2
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
12 7
Sample Input 3 Sample Output 3
2
5 5
0
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的更多相关文章

  1. Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    #29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...

  2. Truck History(prim & mst)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19772   Accepted: 7633 De ...

  3. poj1789 Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  4. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  5. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  6. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  7. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    package car; public class Vehicle { //定义成员变量 private int wheels; private double weight; public int g ...

  8. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  9. Truck History(poj 1789)

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  10. Truck History--poj1789

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21534   Accepted: 8379 De ...

随机推荐

  1. HttpClient拉取连载小说

    上午刚入手的小说,下午心血来潮想从网站上拉取下来做成电子书,呵呵,瞎折腾-说做就做- [抓包] 这一步比什么都重要,如果找不到获取真正资源的那个请求,就什么都不用做了- 先是打算用迅雷把所有页面都下载 ...

  2. p2psearcher绿色版使用方法

    P2pSearcher大家应该很了解,p2p是一款基于P2P技术的资源搜索软件,基于先进的P2P搜索技术,可在瞬间搜遍全球ED2k网络资源,简单便捷的搜索到ED2K网络上共享的海量影音娱乐,学习资料等 ...

  3. vue v-on 带参事件

    1.js代码 var list=[ //定义一个数组 {title:} ] var box=new Vue({ el:'.box', data:{ list:list }, methods:{ //d ...

  4. Android(java)学习笔记36:Scanner类使用

    1. Scanner类使用 package cn.itcast_01; /* * Scanner:用于接收键盘录入数据. * * 前面的时候: * A:导包 * B:创建对象 * C:调用方法 * * ...

  5. Python 函数作为返回值

    函数作为返回值高阶函数除了可以接收函数作为参数外,还可以把函数作为结果值返回. def lazy_sum(*args): def sum(): ax=0 for n in args: ax = ax ...

  6. BZOJ1951:[SDOI2010]古代猪文(Lucas,CRT)

    Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  7. echarts图表与可视窗口的自适应

    由于要适应屏幕尺寸,发现了这个问题.网上搜到了两个办法,如下: 方法一: window.onresize = mychart.resize; 方法二: window.addEventListener( ...

  8. 【洛谷P2324】[SCOI2005]骑士精神

    骑士精神 题目链接 #include<iostream> #include<cstdio> using namespace std; int t,MAXD,sx,sy; ][] ...

  9. 【luoguP1996】【luogu-original】约瑟夫问题

    先来看题目: P1996 约瑟夫问题 题目背景 约瑟夫是一个无聊的人!!! 题目描述 n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人 ...

  10. Android学习笔记_47_SIM卡介绍

    一.判断SIM卡属于哪个移动运营商 1.第一种方法:获取手机的IMSI码,并判断是中国移动\中国联通\中国电信 TelephonyManager telManager = (TelephonyMana ...