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>
#include <queue>
#define LL long long
#define _LL __int64
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 110;

int n,m;
int inter[maxn];
int Map[maxn][maxn];
int dis[maxn],vis[maxn];
int pre[maxn];

void debug()
{
	for(int i = 1; i <= n+1; i++)
	{
		for(int j = 1; j <= n+1; j++)
			printf("%d ",Map[i][j]);
		printf("\n");
	}
}

void spfa()
{
	queue <int> que;
	memset(vis,0,sizeof(vis));
	memset(dis,-INF,sizeof(dis));
	memset(pre,-1,sizeof(pre));

	dis[1] = 0;
	vis[1] = 1;
	que.push(1);

	while(!que.empty())
	{
		int u = que.front();
		que.pop();
		vis[u] = 0;

		for(int v = 1; v <= n+1; v++)
		{
			if(Map[u][v] >= 0&& dis[v] < dis[u] + Map[u][v])
			{
				dis[v] = Map[u][v] + dis[u];
				pre[v] = u;
				if(!vis[v])
				{
					vis[v] = 1;
					que.push(v);
				}
			}
		}
	}
}

void output()
{
	int t = n+1;
	int ans[maxn];
	int cnt = 0;
	while(pre[t] != -1)
	{
		ans[cnt++] = t;
		t = pre[t];
	}

	printf("1");
	for(int i = cnt-1; i >= 1; i--)
		printf("->%d",ans[i]);
	printf("->1\n");
}

int main()
{
	int test;
	int u,v;
	scanf("%d",&test);
	for(int item = 1; item <= test; item++)
	{
		if(item != 1)
			printf("\n");

		scanf("%d",&n);
		for(int i = 1; i <= n; i++)
			scanf("%d",&inter[i]);
		inter[n+1] = 0;

		memset(Map,-1,sizeof(Map));

		scanf("%d",&m);
		while(m--)
		{
			scanf("%d %d",&u,&v);
			Map[u][v] = inter[v];
		}

		spfa();

		printf("CASE %d#\n",item);
		printf("points : %d\n",dis[n+1]);
		printf("circuit : ");
		output();

	}
	return 0;

}

求最长路有点像最长上升子序列,再拿最长上升子序列水一水。

#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#define LL long long
#define _LL __int64
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 110;

int n,m;
int inter[maxn];
int Map[maxn][maxn];
int dis[maxn],vis[maxn];
int pre[maxn];
int dp[maxn];

void solve()
{
	memset(pre,-1,sizeof(pre));

	for(int i = 1; i <= n+1; i++)
	{
		dp[i] = inter[i];
		for(int j = 1; j < i; j++)
		{
			if( Map[j][i] >= 0 && dp[i] < dp[j] + Map[j][i])
			{
				dp[i] = dp[j] + Map[j][i];
				pre[i] = j;
			}
		}
	}

	printf("points : %d\n",dp[n+1]);
	printf("circuit : ");

	int ans[maxn],cnt = 0;
	int t = n+1;
	while(t != -1)
	{
		ans[cnt++] = t;
		t = pre[t];
	}
	ans[cnt] = 1;

	for(int i = cnt; i >= 1; i--)
		printf("%d->",ans[i]);
	printf("%d\n",1);
}

int main()
{
	int test;
	int u,v;
	scanf("%d",&test);
	for(int item = 1; item <= test; item++)
	{
		if(item != 1)
			printf("\n");

		scanf("%d",&n);
		for(int i = 1; i <= n; i++)
			scanf("%d",&inter[i]);
		inter[n+1] = 0;

		memset(Map,-1,sizeof(Map));

		scanf("%d",&m);
		while(m--)
		{
			scanf("%d %d",&u,&v);
			Map[u][v] = inter[v];
		}
		printf("CASE %d#\n",item);
		solve();
	}
	return 0;
}

hdu 1224 Free DIY Tour(最长路/dp),布布扣,bubuko.com

时间: 2024-10-12 10:17:43

hdu 1224 Free DIY Tour(最长路/dp)的相关文章

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

这题真是被弄的欲仙欲死啊,开始写的代码死活的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

Hdu 3696 Farm Game(最长路)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3696 思路:每种商品可以直接卖掉,也可以换购后卖掉.所以设点n+1,从该点向每个商品连边权值为log(p[i])(将乘法转换为加法,直接使用SPFA)表示直接卖掉的单位价值:对于可以换购的商品 i-->j,连 j--> i 权值为log(b[j])的边(反向建图,只需求一次最长路),表示单位 i 商品可以转换为b[j] 的 j 商品(求最长路时逐步累乘转换率).求n+1点到各点的最长路,dist[

hdu 1317 SPFA+连通判断+最长路

Description It has recently been discovered how to run open-source software on the Y-Crate gaming device. A number of enterprising designers have developed Advent-style games for deployment on the Y-Crate. Your job is to test a number of these design

【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 4607 Park Visit(最长路+规律)

Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all