• 1072 Gas Station [dj]


    在这里插入图片描述
    题目大意:找出加油站要尽可能远离居民区 输出这个加油站到居民区最短距离和平均距离

    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #include<map>
    #include<cstring>
    #include<set>
    using namespace std;
    const int maxn = 1011;
    const int inf = 1000000001;
    int n, m, k, ds;
    int d[maxn];
    int G[maxn][maxn];
    bool vis[maxn];
    
    void dj(int s)
    {
    	memset(vis, false, sizeof(vis));
    	fill(d, d + maxn, inf);
    	d[s] = 0;
    	for (int i = 1; i <= n+m; i++)
    	{
    		int u = -1, min = inf;
    		for (int j = 1; j <= n + m; j++)
    		{
    			if (vis[j] == false && d[j] < min)
    			{
    				u = j;
    				min = d[j];
    			}
    		}
    		if (u == -1) return;
    		vis[u] = true;
    		for (int v = 1; v <= n + m; v++)
    		{
    			if (vis[v] == false)
    			{
    				if (d[u] + G[u][v] < d[v])
    				{
    					d[v] = d[u] + G[u][v];
    				}
    			}
    		}
    	}
    }
    int strtoInt(string s)
    {
    	int i = 0, len = s.size(), id = 0;
    	while (i < len)
    	{
    		if (s[i] != 'G')
    		{
    			id = id * 10 + (s[i] - '0');
    		}
    		i++;
    	}
    	if (s[0] == 'G') return n + id;
    	else return id;
    }
    int main()
    {
    	cin >> n >> m >> k >> ds;
    	string s1, s2;
    	int u, v, q;
    	fill(G[0], G[0] + maxn * maxn, inf);
    	for (int i = 1; i <= k; i++)
    	{
    		cin >> s1 >> s2 >> q;
    		u = strtoInt(s1);
    		v = strtoInt(s2);
    		G[u][v] = G[v][u] = q;
    	}
    
    	double Mindis = -1, id = -1, Avq = inf;
    	for (int i = n + 1; i <= n + m; i++)
    	{
    		double mindis = inf, avq = 0;
    		dj(i);
    		for (int j = 1; j <= n; j++)
    		{
    			if (d[j] > ds)
    			{
    				mindis = -1;
    				break;
    			}
    			if (d[j] < mindis)
    			{
    				mindis = d[j];
    			}
    			avq += 1.0 * d[j] / n;
    		}
    		if (mindis == -1) continue;
    		if (mindis > Mindis)
    		{
    			id = i;
    			Mindis = mindis;
    			Avq = avq;
    		}
    		else if (mindis == Mindis && avq < Avq)
    		{
    			id = i;
    			Avq = avq;
    		}
    	}
    	if (id == -1) cout << "No Solution" << endl;
    	else
    	{
    		cout << "G"<< id - n << endl;
    		printf("%.1f %.1f
    ", Mindis, Avq);
    	}
    	return 0;
    }
     
    
  • 相关阅读:
    (转) c++ 迭代器
    (转) 学习C++ -> 向量(vector)
    latex 模版
    javascript继承篇
    ES6的Map和Set的使用,以及weakMap的一点理解
    JavaScript中的eval函数
    Node.js中的进程与线程
    普通函数与箭头函数的区别是什么?
    git 远程拉去代码 输入用户名密码
    npm 设置镜像
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811994.html
Copyright © 2020-2023  润新知