标签:out pos ++i false ems 开始 inf list vector
题意: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; }
UVa 11367 Full Tank? (DP + Dijkstra)
标签:out pos ++i false ems 开始 inf list vector
原文地址:http://www.cnblogs.com/dwtfukgv/p/7742801.html