zoukankan      html  css  js  c++  java
  • Luogu P4009 汽车加油行驶问题

    题目链接 (Click) (Here)

    分层图。。好长时间没写差点要忘了(hhhhh),其实思路还是很明了的。

    注意需要强制消费。

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 110010;
    const int M = 550010;
    #define int long long
    
    int n, k, A, B, C, have, val[110][110];
    
    int node (int x, int y, int f) {
    	return (f - 1) * n * n + (x - 1) * n + y;
    }
    
    bool in_map (int x, int y) {
    	return 1 <= x && x <= n && 1 <= y && y <= n;
    }
    
    int cnt, head[N];
    
    struct edge {
    	int nxt, to, w;
    
    	edge (int _nxt = 0, int _to = 0, int _w = 0) {
    		nxt = _nxt, to = _to, w = _w;
    	}
    }e[M];
    
    void add_edge (int u, int v, int w) {
    	e[++cnt] = edge (head[u], v, w); head[u] = cnt;
    }
    
    int dis[N];
    
    struct Node {
    	int pos, dis;
    	bool operator < (Node rhs) const {return dis > rhs.dis;}
    	Node (int _pos = 0, int _dis = 0) {pos = _pos, dis = _dis;}
    };
    
    priority_queue <Node> q;
    
    int dijkstra (int s, int t) {
    	memset (dis, 0x3f, sizeof (dis));
    	dis[s] = 0; q.push (Node (s, 0));
    	while (!q.empty ()) {
    		Node u = q.top (); q.pop ();
    		if (dis[u.pos] < u.dis) continue;
    		for (int i = head[u.pos]; i; i = e[i].nxt) {
    			int v = e[i].to;
    			if (dis[v] > dis[u.pos] + e[i].w) {
    				dis[v] = dis[u.pos] + e[i].w;
    				q.push (Node (v, dis[v]));
    			}
    		}
    	}
    	return dis[t];
    }
    
    signed main () {
    	cin >> n >> k >> A >> B >> C;
    	for (int i = 1; i <= n; ++i) {
    		for (int j = 1; j <= n; ++j) {
    			cin >> have;
    			val[i][j] = have ? A : A + C;
    		}
    	}
    	int s = node (n, n, k + 1) + 1;
    	int t = node (n, n, k + 1) + 2;
    	add_edge (s, node (1, 1, 1), 0);
    	for (int i = 1; i <= k + 1; ++i) {
    		for (int x = 1; x <= n; ++x) {
    			for (int y = 1; y <= n; ++y) {
    				if (i <= k) {
    					if (i == 1 || val[x][y] != A) {
    						if (in_map (x + 1, y + 0)) add_edge (node (x, y, i), node (x + 1, y + 0, i + 1), 0);
    						if (in_map (x + 0, y + 1)) add_edge (node (x, y, i), node (x + 0, y + 1, i + 1), 0);
    						if (in_map (x - 1, y - 0)) add_edge (node (x, y, i), node (x - 1, y - 0, i + 1), B);
    						if (in_map (x - 0, y - 1)) add_edge (node (x, y, i), node (x - 0, y - 1, i + 1), B);
    					}
    				}
    				add_edge (node (x, y, i), node (x, y, 1), val[x][y]);
    			}
    		}
    		add_edge (node (n, n, i), t, 0);
    	}
    	cout << dijkstra (s, t) << endl;
    } 
    
    
  • 相关阅读:
    web服务器资源分类场景实践
    web服务器动静资源分离及负载均衡调制
    四层负载均衡
    集群中nginx服务的健康检查及负载均衡模板更新
    源码安装nginx及增加模块,nginx的秒级升级,秒级回滚
    phpadmin+redis实现图形化管理数据库笔记
    cookie和session
    移动硬盘修复工具哪个好?怎么修复教程
    硬盘 SMART 检测参数详解[转]
    磁盘显示为GPT(保护分区)
  • 原文地址:https://www.cnblogs.com/maomao9173/p/10534932.html
Copyright © 2011-2022 走看看