• A1018 Public Bike Management [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 = 501;
    const int inf = 10000000;
    int d[maxn], minneed = inf, minback = inf;
    int cp, n,qs,m;
    bool vis[maxn] = { false };	
    int G[maxn][maxn], weight[maxn];
    vector<int>pre[maxn];
    vector<int>tempPath, path;
    
    void djs(int s)
    {
    	fill(d, d + maxn, inf);
    	d[s] = 0;
    	for (int i = 0; i <= n; i++)
    	{
    		int u = -1, mins = inf;
    		for (int j = 0; j <= n; j++)
    		{
    			if (vis[j] == false && d[j] < mins)
    			{
    				u = j;
    				mins = d[j];
    			}
    		}
    		if (u == -1) return;
    
    		vis[u] = true;
    		for (int v = 0; v <= n; v++)
    		{
    			if (vis[v] == false && G[u][v] != inf)
    			{
    				if (d[u] + G[u][v] < d[v])
    				{
    					d[v] = d[u] + G[u][v];
    					pre[v].clear();
    					pre[v].push_back(u);
    				}
    				else if (d[u] + G[u][v] == d[v])
    				{
    					pre[v].push_back(u);
    				}
    			}
    		}
    	}
    }
    void dfs(int v)
    {
    	if (v == 0)
    	{
    		tempPath.push_back(v);
    		int need = 0, back = 0;
    		for (int i = tempPath.size() - 1; i >= 0; i--)
    		{
    			int id = tempPath[i];
    			if (weight[id] > 0)
    			{
    				back += weight[id];
    			}
    			else
    			{
    				if (back > abs(weight[id]))
    				{
    					back -= abs(weight[id]);
    				}
    				else
    				{
    					need += abs(weight[id]) - back;
    					back = 0;
    				}
    			}
    		}
    		if (need < minneed)
    		{
    			minneed = need;
    			minback = back;
    			path = tempPath;
    		}
    		else if (need == minneed && back < minback)
    		{
    			minback = back;
    			path = tempPath;
    		}
    		tempPath.pop_back();
    		return;
    	}
    	tempPath.push_back(v);
    	for (int i = 0; i < pre[v].size(); i++)
    	{
    		dfs(pre[v][i]);
    	}	
    	tempPath.pop_back();
    }	
    
    int main()
    {
    	cin>>cp>>n>>qs>> m;
    	int u, v;
    	fill(G[0], G[0] + maxn * maxn, inf);
    	for (int i = 1; i <= n; i++)
    	{
    		cin >> weight[i];
    		weight[i] -= cp / 2;
    	}
    	for (int i = 0; i < m; i++)
    	{
    		cin >> u >> v;
    		cin >> G[u][v];
    		G[v][u] = G[u][v];
    	}
    	djs(0);
    	dfs(qs);
    	cout << minneed << " ";
    	for (int i = path.size() - 1;i >= 0; i--)
    	{
    		cout << path[i];
    		if (i > 0) cout << "->";
    	}
    	cout << " " << minback << endl;
    	return 0;
    }
    
    
  • 相关阅读:
    ReactiveCocoa RACObserve subscribeNext 时,只有值不一样时才响应
    ReactiveCocoa 监听Enabled和添加Command出错的处理方法
    Masonry + UIView Animations 注意事项
    addObserver forKeyPath options 注意事项
    ios中tabbar得title和navigationbar的title如何修改
    tableview 分组显示返回footerviewt和headerView的高度不能为0的问题
    UITableViewCell的选中时的颜色设置
    ios 枚举 位移操作
    设置UIButton 字体 颜色
    jsoup 源码分析
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811995.html
Copyright © 2020-2023  润新知