程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1676 Full Tank? 限制最短路(difficult)

HDU 1676 Full Tank? 限制最短路(difficult)

編輯:C++入門知識

點擊打開鏈接

Full Tank?

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 155 Accepted Submission(s): 71


Problem Description After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?
To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.

Input The first line of input gives 1 <= n <= 1000 and 0 <= m <= 10000, the number of cities and roads. Then follows a line with n integers 1 <= pi <= 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 <= u, v < n and 1 <= d <= 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 <= q <= 100, giving the number of queries, and q lines with three integers 1 <= c <= 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.

Output For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or “impossible” if there is no way of getting from s to e with the given car.
Sample Input
5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4

Sample Output
170
impossible

Source 2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
有n個城市和m條路,一輛車要從s城市到e城市,而且這輛車能夠乘的油的容量為cap,每走1單位距離就耗費1單位油。每個城市都有加油站,但是每個加油站的價格不同。讓你判斷這輛車能否到達城市e,如果能夠達到,那麼所要耗費的最少價格是多少。 每個城市油的價格不同,所以單純的求最短路肯定行不通。用dp[i][j]來維護cost,代表到達到達第i個城市時,油箱的剩余油量為j時的最小花費。然後對於每個城市油量都是+1,然後更新與此城市相連的城市。
//250MS	4900K
#include
#include
#include
#include
#include
#include
#define M 1007
using namespace std;
int n,m,cap,s,e,num;
int val[M],dp[M][M],head[M];
struct node
{
    int u,cost,res;//u代表節點,cost代表當前節點的花費,res代表此節點剩余的油
    bool operator < (const node a)const
    {
        return cost>a.cost;
    }
};
struct E
{
    int v,to,dist;
} edg[M*20];
vectorG[M];
void addedge(int u,int v,int dist)
{
    edg[num].v=v;
    edg[num].dist=dist;
    edg[num].to=head[u];
    head[u]=num++;
}
void init()
{
    num=0;
    memset(head,-1,sizeof(head));
    memset(val,0,sizeof(val));
    for(int i=0; i q;
    q.push((node){s,0,0});
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        int u=x.u,cost=x.cost,res=x.res;
        if(u==e)
        {
            printf("%d\n",cost);
            return ;
        }
        if(rescost+val[u]))//如果多加1單位油,能夠花費更少
        {
            dp[u][res+1]=cost+val[u];
            q.push((node){u,dp[u][res+1],res+1});
        }
        for(int i=head[u]; i!=-1; i=edg[i].to)//更新與此節點相鄰的節點
        {
            int v=edg[i].v,dist=edg[i].dist;
            if(rescost)//能夠達到下一個節點,而且花費更少
            {
                dp[v][res-dist]=cost;
                q.push((node){v,cost,res-dist});
            }
        }
    }
    printf("impossible\n");
    return ;
}
int main()
{
    while( scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=0; i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved