zoukankan      html  css  js  c++  java
  • 网络流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
    ", cns);
    	return 0;
    }
    

      

    寻找真正的热爱
  • 相关阅读:
    js遍历table,gridview
    JavaScript和Webservice实现联动
    DataTable应用
    POJ1039+几何+直线于线段相交
    POJ2398+几何+排序
    HDU4506+快速求幂
    HDU4536+DFS
    POJ3304+几何
    POJ3555+几何+半平面交
    HDU4544+优先队列
  • 原文地址:https://www.cnblogs.com/lesning/p/11729184.html
Copyright © 2011-2022 走看看