zoukankan      html  css  js  c++  java
  • UVA11367 Full Tank? 【分层图最短路】

    题目

    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.

    输入格式

    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.

    输出格式

    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.

    输入样例

    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

    输出样例

    170
    impossible

    题解

    我们把点和在该点拥有的油量看做一个状态,建立分层图
    每个点向其相邻的油量减少量为路程长的点连边【费用0】,向其自身油量+1的点连边【费用为油价】
    跑dijkstra【可能SPFA跑分层图什么的会快一些】
    复杂度(O(q * n * c * log(n + c)))

    注意每一只加1升油,不能随意加油,否则就会退化到(O(q*n*c^2*log(n + c)))

    #include<iostream>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long int
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
    using namespace std;
    const int maxn = 1005,maxm = 20005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
    	return out * flag;
    }
    int n,m,h[maxn],ne = 2;
    struct EDGE{int to,nxt,w;}ed[maxm];
    void build(int u,int v,int w){
    	ed[ne] = (EDGE){v,h[u],w}; h[u] = ne++;
    	ed[ne] = (EDGE){u,h[v],w}; h[v] = ne++;
    }
    int d[maxn][105],p[maxn],vis[maxn][105];
    struct node{int u,c,f;};
    inline bool operator <(const node& a,const node& b){return a.f > b.f;}
    priority_queue<node> q;
    int solve(int S,int T,int C){
    	for (int i = 1; i <= n; i++)
    		for (int j = 0; j <= C; j++)
    			d[i][j] = INF,vis[i][j] = 0;
    	d[S][0] = 0;
    	q.push((node){S,0,d[S][0]});
    	int v; node u;
    	while (!q.empty()){
    		u = q.top(); q.pop();
    		if (vis[u.u][u.c]) continue;
    		vis[u.u][u.c] = true;
    		Redge(u.u)
    			if (ed[k].w <= u.c && !vis[to = ed[k].to][v = u.c - ed[k].w] && d[to][v] > d[u.u][u.c]){
    				d[to][v] = d[u.u][u.c]; q.push((node){to,v,d[to][v]});
    			}
    		if (!vis[u.u][u.c + 1] && d[u.u][u.c + 1] > d[u.u][u.c] + p[u.u]){
    			d[u.u][u.c + 1] = d[u.u][u.c] + p[u.u];
    			q.push((node){u.u,u.c + 1,d[u.u][u.c + 1]});
    		}
    	}
    	int ans = INF;
    	for (int i = 0; i <= C; i++) ans = min(ans,d[T][i]);
    	return ans;
    }
    int main(){
    	n = read(); m = read();
    	REP(i,n) p[i] = read();
    	int a,b,w;
    	while (m--){
    		a = read() + 1; b = read() + 1; w = read();
    		build(a,b,w);
    	}
    	int q = read(),S,T,C,ans;
    	while (q--){
    		C = read(); S = read() + 1; T  = read() + 1;
    		ans = solve(S,T,C);
    		if (ans == INF) puts("impossible");
    		else printf("%d
    ",ans);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    面向对象进阶
    EasyDSS转码模块关于gRPC服务注册到ETCD的实现过程
    如何通过ETCD实现EasyDSS分布式负载均衡?ETCD部署方案
    EasyDSS_dash版本如何在新内核下实现rtsp源地址的分发?
    EasyDSS因为系统时间导致的闪退分析及处理
    EasyScreenLive推流组件推RTSP流到EasyDSS操作过程分享
    【解决方案】无人机+EasyDSS实现直播推流警务安防类行业应用
    EasyDSS转码服务负载均衡采用grpc balance回报找不到结构体问题排查及修复
    EasyDSS视频直播时直播间接口和实际接口为什么会存在差异?
    在线课堂EasyDSS_dash版本虚拟直播RTSP播放无视频流问题
  • 原文地址:https://www.cnblogs.com/Mychael/p/8403915.html
Copyright © 2011-2022 走看看