POJ 1129/ZOJ 1084

本文介绍了使用深度优先搜索(DFS)解决通道分配问题的方法,包括精确解法和贪心解法。通过实例展示了如何应用DFS算法来解决实际问题,并讨论了贪心解法的局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搜索练习,没什么重要的剪枝要考虑,直接按照dfs模板码就行了!但是网上也有贪心解法,虽然不能确定得到最优解,但是能够ac!

精确解法代码如下:

#include <iostream>
#include <vector>
using namespace std;
int n;
vector<vector<int> > adjList;
vector<int> color;
int corNum;
bool Judge(int l, int c)
{
    int i = 0;
    while( i < adjList[l].size() )
    {
	if( c == color[adjList[l][i]] )
	    return false;
	++i;
    }
    return true;
}
bool Dfs(int l)
{
    if( l == n )
	return true;
    for(int i = 1; i <= corNum; ++i)
    {
	if( Judge(l, i) )
	{
	    color[l] = i;
	    if( Dfs(l+1) )
		return true;
	    color[l] = 0;
	}
    }
    return false;
}

int main()
{
    while( cin >> n && n )
    {
	cin.get();
	adjList.clear();
	adjList.resize( n );
	color.assign(n, 0);
	for(int i = 0; i < n; ++i)
	{
	    int u = cin.get() - 'A';
	    cin.get();
	    char ch;
	    while( (ch = cin.get()) != '\n' )
		adjList[u].push_back( ch - 'A' );
	}
	corNum = 1;
	while( !Dfs(0) )
	    ++corNum;
	cout << corNum << " channel";
	cout << ((corNum == 1)?" needed.":"s needed.") << endl;
    }
    return 0;
}

贪心解法(虽然能a,但不是正确解法)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector<vector<int> > adjList;
vector<int> color;
int Dfs(int node, int cnt)
{
    if(node == n)
	return cnt;
    vector<bool> c( cnt + 2, false );
    for(int i = 0; i < adjList[node].size(); ++i)
    {
	int v = adjList[node][i];
	c[color[v]] = true;
    }
    int ans = 0xffff;
    for(int i = 1; i <= cnt + 1; ++i)
    {
	if( !c[i] )
	{
	    color[node] = i;
	    int corNum = (i < cnt+1)?cnt:cnt+1;
	    int temp = Dfs(node+1, corNum);
	    ans = min(ans, temp);
	    color[node] = 0;
	}
    }
    return ans;
}
void Work()
{
    int ans = Dfs(0, 0);
    cout << ans << " " ;
    cout << (ans==1?"channel needed.":"channels needed.") << endl;
}

int main()
{
    while( cin >> n && n)
    {
	adjList.clear();
	adjList.resize( n );
	color.assign(n, 0);
	cin.get() ;
	for(int i = 0; i < n; ++i)
	{
	    int s = cin.get() - 'A';
	    cin.get();
	    char ch ;
	    while( (ch = cin.get()) != '\n')
	    {
		adjList[s].push_back( ch - 'A' );
	    }
	}
	/*
	 * first solution : greedy algorithm
	for(int i = 0; i < n; ++i)
	{
	    vector<bool> corSign(n+1, 0);
	    for(int j = 0; j < adjList[i].size(); ++j)
	    {
		int c = color[adjList[i][j]];
		c?corSign[c]=true:NULL;
	    }
	    for(int j = 1; j <= n; ++j)
		if( !corSign[j] )
		{
		    color[i] = j;
		    break;
		}
	}
	int ans = *max_element(color.begin(), color.end());
	cout << ans << " " ;
	cout << (ans==1?"channel needed.":"channels needed.") << endl;
	*/
	/*
	 * 上面那个贪心不能解决http://zhyu.me/acm/poj-1129.html中提到的一个
	 * 例子,下面的dfs和greedy结合貌似可以,但是仍然不能保证得到最优解
	 */
	Work();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值