首页 技术 正文
技术 2022年11月7日
0 收藏 517 点赞 682 浏览 2543 个字

题意:n个城市有m条道路。每个城市的油价不一样,给出起点s和终点t,以及汽车的油箱的容量,求从城市s到城市 t 的最便宜路径。

析:dp[u][i] 表示在第 u 个城市,还剩下 i L升油,一开始用BFS,TLE,要注意效率,用dijkstra,找到城市 t 就该结束了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}int pri[maxn];
struct Edge{
int to, val, next;
};
Edge edges[maxn*10<<1];
int head[maxn], cnt;void addEdge(int u, int v, int c){
edges[cnt].to = v;
edges[cnt].val = c;
edges[cnt].next = head[u];
head[u] = cnt++;
}int dp[maxn][105];struct HeapNode{
int cost, u, last;
bool operator < (const HeapNode &p) const{
return cost > p.cost;
}
};int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i) scanf("%d", pri + i);
ms(head, -1); cnt = 0;
while(m--){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
addEdge(u, v, c);
addEdge(v, u, c);
}
scanf("%d", &m);
while(m--){
int s, t, c;
scanf("%d %d %d", &c, &s, &t);
for(int i = 0; i < n; ++i) ms(dp[i], INF);
dp[s][0] = 0;
priority_queue<HeapNode> pq;
pq.push((HeapNode){0, s, 0});
bool ok = false;
while(!pq.empty()){
HeapNode h = pq.top(); pq.pop();
if(h.u == t){ printf("%d\n", h.cost); ok = true; break; }
int u = h.u, last = h.last, cost = h.cost;
if(last < c && dp[u][last+1] > cost + pri[u]){
dp[u][last+1] = cost + pri[u];
pq.push((HeapNode){cost + pri[u], u, last + 1});
}
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(last >= edges[i].val && dp[v][last-edges[i].val] > cost){
dp[v][last-edges[i].val] = cost;
pq.push((HeapNode){cost, v, last-edges[i].val});
}
}
}
if(!ok) puts("impossible");
}
}
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905