zoukankan      html  css  js  c++  java
  • 汽车加油行驶问题(最短路)

    //http://www.cnblogs.com/IMGavin/
    #include <iostream>
    #include <stdio.h>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <map>
    #include <stack>
    #include <set>
    #include <bitset>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    #define gets(A) fgets(A, 1e8, stdin)
    const int INF = 0x3F3F3F3F, N = 108, MOD = 1003, M = 12;
    
    const double EPS = 1e-6;
    
    int dis[N][N][M];
    bool inq[N][N][M];
    int mp[N][N];
    int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    int n, k, a, b, c;
    
    struct node{
    	int x, y, k;
    	node(int x1, int y1, int k1){
    		x = x1;
    		y = y1;
    		k = k1;
    	}
    
    };
    
    int SPFA(){
    	memset(dis, 0x3F, sizeof(dis));
    	memset(inq, 0, sizeof(inq));
    	dis[1][1][k] = 0;
    	queue <node> q;
    	q.push(node(1, 1, k));
    	while(!q.empty()){
    		node u = q.front();
    		q.pop();
    		inq[u.x][u.y][u.k] = 0;
    		for(int i = 0; i < 4; i++){
    			int x1 = u.x + dir[i][0];
    			int y1 = u.y + dir[i][1];
    			if(x1 < 1 || y1 < 1 || x1 > n || y1 > n || u.k == 0){
    				continue;
    			}
    			
    			node v(x1, y1, u.k - 1);
    			int cost = dis[u.x][u.y][u.k];
    			if(dir[i][0] < 0 || dir[i][1] < 0){
    				cost += b;
    			}
    			if(!mp[v.x][v.y] && dis[v.x][v.y][v.k] > cost){
    				dis[v.x][v.y][v.k] = cost;
    				if(!inq[v.x][v.y][v.k]){
    					inq[v.x][v.y][v.k] = 1;
    					q.push(v);
    				}
    			}
    			
    			v.k = k;
    			cost = dis[u.x][u.y][u.k] + a;
    			if(dir[i][0] < 0 || dir[i][1] < 0){
    				cost += b;
    			}
    			if(!mp[v.x][v.y]){
    				cost += c;
    			}
    
    			if(dis[v.x][v.y][v.k] > cost){
    				dis[v.x][v.y][v.k] = cost;
    				if(!inq[v.x][v.y][v.k]){
    					inq[v.x][v.y][v.k] = 1;
    					q.push(v);
    				}
    			}
    
    		}
    	}
    	int ans = INF;
    	for(int i = 0; i <= k; i++){
    		ans = min(ans, dis[n][n][i]);
    	}
    	return ans;
    }
    
    int main(){
    	while(cin >> n >> k >> a >> b >> c){
    		for(int i = 1; i <= n; i++){
    			for(int j = 1; j <= n; j++){
    				scanf("%d", &mp[i][j]);
    			}
    		}
    		cout<<SPFA()<<endl;
    	}
    
    	return 0;
    }
    

      

  • 相关阅读:
    根据类生成数据库连接
    C# 获取动态类中所有的字段
    mysql 基础配置经验
    CSS小笔记
    jquery知识location.search
    Eclipse 启动tomcat 访问主页报错404
    window下安裝redis服務
    用maven创建web工程
    @WebListener 注解方式实现监听
    Dubbo-admin管理平台的安装
  • 原文地址:https://www.cnblogs.com/IMGavin/p/6392171.html
Copyright © 2011-2022 走看看