网络流24题---- 汽车加油行驶问题

分享图片

 

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 300100
using namespace std;
const int N = 110;
const int base = 20010;
int n, k, a, b, c;

struct Node {
	int p;
	int len;
	Node(int a, int b) :p(a), len(b) {}
};
vector<Node>G[maxn];
void insert(int be, int en, int len) {
	G[be].push_back(Node(en, len));
}

int vis[maxn];
int dis[maxn];
int spfa(int be) {
	queue<int>que;
	for (int i = 0; i < maxn; i++) {
		vis[i] = 0;
		dis[i] = 2000000000;
	}
	que.push(be);
	dis[be] = 0;
	while (!que.empty()) {
		int x = que.front();
		que.pop();
		vis[x] = 0;
		for (int i = 0; i < G[x].size(); i++) {
			int p = G[x][i].p;
			if (dis[p] > dis[x] + G[x][i].len) {
				dis[p] = dis[x] + G[x][i].len;
				if (!vis[p]) {
					que.push(p);
					vis[p] = 1;
				}
			}
		}
	}
	return 0;
}
int main() {
	scanf("%d %d %d %d %d", &n, &k, &a, &b, &c);
	int flag;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			int num = i * N + j;
			scanf("%d", &flag);
			if (!flag) {
				for (int s = 0; s < k; s++) {//每一层
					int chal = s * base;
					//向前
					insert(num + chal, ((i + 1)*N + j) + chal + base, 0);
					insert(num + chal, (i*N + j + 1) + chal + base, 0);
					insert(num + chal, ((i - 1)*N + j) + chal + base, b);
					insert(num + chal, i*N + j - 1 + chal + base, b);
				}
				for (int s = 1; s <= k; s++) {
					int chal = s * base;
					insert(num + chal, num, a + c);
				}
			}
			else {
				for (int s = 1; s <= k; s++) {
					int chal = s * base;
					insert(num + chal, num, a);
				}
				insert(num, ((i + 1)*N + j) + base, 0);
				insert(num, (i*N + j + 1) + base, 0);
				insert(num, ((i - 1)*N + j) + base, b);
				insert(num, i*N + j - 1 + base, b);
			}
		}
	}
	spfa(N + 1);
	int cns = 2000000000;
	for (int i = 0; i <= k; i++) {
		cns = min(dis[n*N + n + i * base], cns);
	}
	printf("%d\n", cns);
	return 0;
}
相关文章
相关标签/搜索