zoukankan      html  css  js  c++  java
  • [POJ3635] Full Tank?

    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

    Nordic 2007

    题解

    先简要说一下题意:(N)个点,(M)条边,每一条边都要消耗一定的油量,每一个点可以加不定量的油,且有一定的油费,现在给定油桶的容量,要从编号为(s)的点到编号为(t)的点,求最小费用

    很明显的最短路,我用的是(Dijkstra),油一升一升的加即可

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<queue>
    using namespace std;
    
    const int N=2500,M=25000,C=150;
    int n,m,c,s,t,p[N];
    struct edge
    {
    	int v,w,nx;
    }e[M];
    int ne,h[N];
    struct node
    {
    	int id,w,c;
    	bool operator< (node tmp) const
    	{
    		return w>tmp.w;
    	}
    };
    priority_queue<node> Q;
    int v[N][C];
    
    void Build(int u,int v,int w)
    {
    	++ne,e[ne]=(edge){v,w,h[u]},h[u]=ne;
    }
    
    void Read()
    {
    	scanf("%d%d",&n,&m);
    	for(int i=0;i<n;++i) scanf("%d",&p[i]);
    	int u,v,w;
    	for(int i=1;i<=m;++i)
    	{
    		scanf("%d%d%d",&u,&v,&w);
    		Build(u,v,w),
    		Build(v,u,w);
    	}
    }
    
    void Dijkstra()
    {
    	for(int i=0;i<n;++i)
    		for(int j=0;j<=c;++j) v[i][j]=0;
    	while(!Q.empty()) Q.pop();//注意还原
    	Q.push((node){s,0,0});
    	node tp;
    	while(!Q.empty())
    	{
    		tp=Q.top();
    		Q.pop();
    		if(v[tp.id][tp.c]) continue;
    		v[tp.id][tp.c]=1;
    		if(tp.id==t)
    		{
    			printf("%d
    ",tp.w);
    			return;
    		}
    		if(tp.c<c) Q.push((node){tp.id,tp.w+p[tp.id],tp.c+1});
    		for(int i=h[tp.id];i;i=e[i].nx)
    			if(tp.c>=e[i].w)
    				Q.push((node){e[i].v,tp.w,tp.c-e[i].w});
    	}
    	puts("impossible");
    }
    
    int main()
    {
    	int T;
    	Read();
    	scanf("%d",&T);
    	for(int i=1;i<=T;++i)
    	{
    		scanf("%d%d%d",&c,&s,&t),
    		Dijkstra();
    	}
    	return 0;
    }
    
  • 相关阅读:
    放大图 带回弹效果
    用recyclerview实现对话(通过接口实现)
    java 问号表达式
    recyclerview的使用
    我遇到的报错信息整理
    横竖屏切换
    NOIP 2017提高组自测 奶酪
    小蝌蚪找妈妈 牛客
    牛客练习赛50 C tokitsukaze and Soldier
    洛谷P1630 求和
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12038824.html
Copyright © 2011-2022 走看看