标签:rip ref void maximum big oca proc 保留 esc
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.
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.
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 */
标签:rip ref void maximum big oca proc 保留 esc
原文地址:https://www.cnblogs.com/-xiangyang/p/9824754.html