C - 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.
\includegraphics[width=.7\textwidth ]{graph}
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 1
to location 6. If you follow the path 1→2→3→6, the length is 9, and you’ll get to pick up 4 items. Of course, it would be better to drive 1→4→5→6; that’s still a length of 9, but going this way instead lets you pick up an additional item. Driving 1→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, n
(2≤n≤100), giving the number of locations in the city. Locations are numbered from 1 to n, with location 1 being the starting location and n being the destination. The next input line gives a sequence of n integers, t1…tn, where each ti indicates the number of items your friends have asked you to pick up from location i. All the ti values are between 0 and 100, inclusive. The next input line contains a non-negative integer, m, giving the number of roads in the city. Each of the following m lines is a description of a road, given as three integers, abd. This indicates that there is a road of length d between location a and location b. The values of a and b are in the range 1…n, and the value of d is between 1 and 100

, 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 1
to location n, just output out the word “impossible”. Otherwise, output the length of a shortest path from location 1 to location n, followed by the maximum number of items you can pick up along the way.

#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 minn,V=-1,k;
    for (int i = 1; i <=n ; ++i)
    {
        minn=inf;
        k=V;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&minn>dis[j])
            {
                minn=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]==inf)
        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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

直接AC好吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值