码迷,mamicode.com
首页 > 其他好文 > 详细

uva-103-dp----has error

时间:2019-05-12 22:30:24      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:for   map   sharp   discuss   uva   ESS   int   mes   ext   

  题意:矩阵嵌套,DAG图DP

下面的代码在hdu过不了,voj能过.详见http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=38519&messageid=1&deep=0

 

#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
	using std::cout;
	using std::endl;
	using std::cin;
	using std::map;
	using std::vector;
	using std::string;
	using std::sort;
	using std::priority_queue;
	using std::greater;
	using std::vector;
	using std::swap;
	using std::stack;
	using std::queue;
	using std::bitset;


	constexpr int N = 64;
	constexpr int D = 11;

	int k;
	int n;
	class Node
	{
	public:
		int d[16] = { 0 };
		int index;
		const bool operator < (const Node& n2) const
		{
			for (int i = 0;i < k;i++)
			{
				if (this->d[i] == n2.d[i])
					continue;
				if (this->d[i] > n2.d[i])
					return false;
				else return true;
			}
			return false;
		}
		
	};
	Node nodes[N];


	int m[N][N];
	int dp[N];
	int path[N];

	void build()
	{
		for (int i = 0;i < n;i++)
		{
			for (int j = i + 1;j < n;j++)
			{
				int ok = 1;
				for (int t = 0;t < k;t++)
				{
					if (nodes[i].d[t] >= nodes[j].d[t])
					{
						ok = 0;
						break;
					}
				}
				if (ok)
				{
					m[i][j] = 1;
				}
			}
		}
	}





	int first = 1;
	
	void print(int maxIndex,int max) 
	{
		if (max == 0)
		{
			return;
		}
			
		print(path[maxIndex],max-1);
		if (first)
		{
			cout << nodes[path[maxIndex]].index;
			first = 0;
		}
		else
		{
			cout << " " << nodes[path[maxIndex]].index;
		}
	}

	void solve()
	{
	
		while (cin>>n>>k) 
		{
			if (n == 0 && k == 0)
				return;
			for (int i=0;i<n;i++) 
			{
				Node in;
				in.index = i + 1;
		
				for (int j=0;j<k;j++) 
				{
					cin >> in.d[j];
				}
				sort(in.d,in.d+k);
				nodes[i] = in;
			}
			sort(nodes,nodes+n);
			//bubbleSort();
			memset(m, 0, sizeof(m));
			build();
			memset(dp,0,sizeof(dp));
			memset(path,0,sizeof(path));
			int max = -1;
			int maxIndex = -1;
			first = 1;
			for (int i=0;i<n;i++) 
			{
				int curMax = 1;
				for (int j=0;j<i;j++) 
				{
					if (m[j][i] == 0)
						continue;
					if (1 + dp[j] > curMax)
					{
						path[i]=j;
						curMax = 1 + dp[j];
					}
				}
				dp[i] = curMax;
				if (curMax > max)
				{
					max = curMax;
					maxIndex = i;
				}
			}
			cout << max << endl;
			//cout
			print(maxIndex, max-1);
			if(first)
			cout<<nodes[maxIndex].index << endl;
			else
			{
				cout<<" " << nodes[maxIndex].index << endl;

			}
		}
	}

};



int main()
{

#ifndef ONLINE_JUDGE
	freopen("d://1.text", "r", stdin);
	//freopen("d://1.out", "w", stdout);
#endif // !ONLINE_JUDGE
	cc::solve();
	return 0;
}

  

uva-103-dp----has error

标签:for   map   sharp   discuss   uva   ESS   int   mes   ext   

原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10854004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!