hdu 1224 Free DIY Tour

这题真是被弄的欲仙欲死啊,开始写的代码死活的a不掉,也不知道哪里错了,先附上,求大牛指点啊.....

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int n,m;
int point[100+5];
int re;
int ree[105];
int l;
struct stu
{
	int a,b;
};
stu mapp[100000+5];
void dfs(int x,int sum,int y[],int k)//搜索
{
	y[k]=mapp[x].a;
	k++;
	if(mapp[x].b==n+1)
	{
		y[k]=1;
		if(sum>re)
		{
			re=sum;
			l=k;
			for(int i=0;i<=k;i++)
			{
				ree[i]=y[i];
			}
		}
		return;
	}
	sum+=point[mapp[x].b];
	for(int i=x+1;i<m;i++)
	{
		if(mapp[x].b==mapp[i].a&&mapp[i].a<mapp[i].b) dfs(i,sum,y,k);
	}
}
bool cmp(stu x,stu y)
{
	return x.a <y.a;
}
int main()
{
	cin.sync_with_stdio(false);
	int t;
	cin>>t;
	int s;
	for(s=1;s<=t;s++)
	{
		cin>>n;
		for(int i=1;i<=n;i++) cin>>point[i];//输入
		cin>>m;
		for(int i=0;i<m;i++) cin>>mapp[i].a>>mapp[i].b;//输入
		re=-1;
		if(s!=1) cout<<endl;
		sort(mapp,mapp+m,cmp);//排序
	    for(int i=0;i<m;i++)
	    {
	    	int mem[205];
	    	if(mapp[i].a==1&&mapp[i].a<mapp[i].b)//搜索
	    	dfs(i,0,mem,0);
	    }
	    cout<<"CASE "<<s<<"#"<<endl;
	    cout<<"points :"<<" "<<re<<endl;
	    printf("circuit : %d",ree[0]);
	    for(int i=1;i<=l;i++) printf("->%d",ree[i]);
	    cout<<endl;
	}
	return 0;
}

之后用动态数组重写,回溯记录路径才过了,被弄的不要不要的> . <!

下面是ac代码

#include<iostream>
#include<vector>
#include<cstring>
#define maxn 105
using namespace std;
vector<int>mapp[maxn];
int point[maxn];
int visit[maxn];
int ans[maxn];
int rem[maxn];
int n,m;
int re;
void dfs(int d,int sum)
{
	if(d==n+1)
	{
		if(re<sum)
		{
			re=sum;
			for(int i=0;i<maxn;i++) ans[i]=rem[i];
		}
	}
	for(int i=0;i<mapp[d].size();i++)
	{
		int next=mapp[d][i];
		if(!visit[next])
		{
			visit[next]=1;
			rem[next]=d;
			dfs(next,sum+point[next]);
			visit[next]=0;
		}
	}
}
void print(int x)
{
	if(x==1) cout<<1;
	else
	{
		print(ans[x]);
		if(x==n+1) x=1;
		cout<<"->"<<x;
	}
}
int main()
{
	int s,t;
	cin>>t;
	for(s=1;s<=t;s++)
	{
		for(int i=0;i<maxn;i++) mapp[i].clear();
		cin>>n;
		for(int i=1;i<=n;i++) cin>>point[i];
		point[n+1]=0;
		cin>>m;
		for(int i=0;i<m;i++)
		{
			int x,y;
			cin>>x>>y;
			mapp[x].push_back(y);
		}
		visit[1]=1;
		re=-1;
		memset(visit,0,sizeof(visit));
		dfs(1,0);
		cout<<"CASE "<<s<<"#"<<endl;
		cout<<"points : "<<re<<endl;
		cout<<"circuit : ";
		print(n+1);
		cout<<endl;
		if(s!=t) cout<<endl;

	}
	return 0;
}
时间: 2024-10-29 05:05:38

hdu 1224 Free DIY Tour的相关文章

hdu 1224 Free DIY Tour(最长路/dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1224 基础的求最长路以及记录路径.感觉dijstra不及spfa好用,wa了两次. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #include <string.h> #

HDU 1224 Free DIY Tour(DP)

Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It's

HDU 1224 Free DIY Tour 简单DP

Free DIY Tour Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around th

hdu 1224 Free DIY Tour(最长的公路/dp)

http://acm.hdu.edu.cn/showproblem.php? pid=1224 基础的求最长路以及记录路径. 感觉dijstra不及spfa好用,wa了两次. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #include <string.h>

【HDOJ】1224 Free DIY Tour

DP. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 8 #define MAXN 105 9 int score[MAXN]; 10 bool map[MAXN][MAXN]; 11 int path[MAXN]; 12 int

HDU 1224 Free DIY Tour--DP--(bug集锦)

题意:有多个城市编号为1到 n分别有不同的评分,现在知道了某些城市之间的直航,找一条从起点出发回到起点.沿途经过的城市总评分最大的路径.要求只能从编号低的             到编号高的,所以起点编号为1和n+1 分析:dp[i]表示从1到i的最优解,dp[i]=max(dp[j]+intr[i]),其中 1<= j< i (这个条件可以保证只从编号低的走到编号高的) 这题一开始思路就是对的,不过不停WA,一路排查了各种bug,其中最重要的两个:1.long long 2.多组case 和

hdu 1224(动态规划 DAG上的最长路)

Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5815    Accepted Submission(s): 1855 Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfi

HDU1224 Free DIY Tour 【SPFA】

Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3939    Accepted Submission(s): 1262 Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulf

F - Free DIY Tour(动态规划)

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的 Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free