zoukankan      html  css  js  c++  java
• UVa 11367 Full Tank? (DP + Dijkstra)

题意：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];

void addEdge(int u, int v, int c){
edges[cnt].to = v;
edges[cnt].val = c;
}

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);
while(m--){
int u, v, c;
scanf("%d %d %d", &u, &v, &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
", 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;
}
```

• 相关阅读:
优先队列的一种实现方式——堆
iOS 批量打包
Xcode 8 的 Debug 新特性 —- WWDC 2016 Session 410 & 412 学习笔记
仿淘宝上拉进入详情页交互的实现
iOS 10 的适配问题
集成支付宝钱包支付 iOS SDK 的方法与经验
Quick Touch – 在 iOS 设备运行的 “Touch Bar”
iOS 开发中的 Tips（一）
ReactiveCocoa 中 RACSignal 是怎样发送信号
自定义下拉刷新控件
• 原文地址：https://www.cnblogs.com/dwtfukgv/p/7742801.html