首先来总结一下:此次湖大之行可谓是惨不忍睹!虽然全程5个小时,虽然没有浪费什么,可是没拿到一个Accept!忘掉悲伤,继续前行。经历过失败,站起来,下一次不会在同一个地方跌倒,前行的路上难免有磕磕绊绊,其实最能让人回忆的是那些泪水 和 欢笑。
下面步入主题:
Problem G
DescriptionTravel in time
Bob gets tired of playing games, leaves Alice, and travels to Changsha alone.
Yuelu Mountain , Orange Island, Window of the World, the Provincial Museum
etc...are scenic spots Bob wants to visit. However, his time is very limited, he can’t
visit them all.
Assuming that there are N scenic spots in Changsha, Bob defines a satisfaction
value Si to each spot. If he visits this spot, his total satisfaction value will plus Si. Bob
hopes that within the limited time T, he can start at spot S, visit some spots selectively,
and finally stop at spot E, so that the total satisfaction value can be as large as possible.
It's obvious that visiting the spot will also cost some time, suppose that it takes Ci
units of time to visit spot i ( 0 <= i < N ).
Always remember, Bob can choose to pass by a spot without visiting it (including
S and E), maybe he just want to walk shorter distance for saving time.
Bob also has a special need which is that he will only visit the spot whose
satisfaction value is strictly larger than that of which he visited last time. For example,
if he has visited a spot whose satisfaction value is 50, he would only visit spot whose
satisfaction value is 51 or more then. The paths between the spots are bi-directional,
of course.
Input
The first line is an integer W, which is the number of testing cases, and the W sets
of data are following.
The first line of each test data contains five integers: N M T S E. N represents the
number of spots, 1 < N < 100; M represents the number of paths, 0 < M < 1000; T
represents the time limitation, 0 < T <= 300; S means the spot Bob starts from. E
indicates the end spot. (0 <= S, E < N)
The second line of the test data contains N integers Ci ( 0 < Ci <= T ), which
means the cost of time if Bob visits the spot i .
The third line also has N integers, which means the satisfaction value Si that can
be obtained by visiting the spot i ( 0 <= Si < 100 ).
The next M lines, each line contains three integers u v L, means there is a
bi-directional path between spot u and v and it takes L units of time to walk from u to
v or from v to u. (0 <= u, v < N, 0 < L <= T)
Output
Output case number in the first line (formatted as the sample output).
The second line contains an integer, which is the greatest satisfaction value.
If Bob can’t reach spot E in T units of time, you should output just a “0” (without
quotation marks).
Sample Input
14
4 22 0 3
1 1 1 1
5 7 9 12
0 1 10
1 3 10
0 2 10
2 3 10
Sample Output
Case #1:
21
我的思路:
由于时间有限,bob只选择性的游玩一些景点。当他游玩游景点时,会获得相应的满意价值。
但他有两个要求:
1.Bob给每一个景点定义了一个满意价值,
Bob在游玩时,他到达的下一个景点的满意值必须大于当前景点的满意值,(可以理解为bob旅游景点的满意值是一个严格递增
下面来看一下测试用例:
bob 有两条路线:A 0——>1——>3 和B 0——>2——>3 每条边的时间消耗是10个时间单位,每个顶点时间消耗是1个时间单位,在22个时间单位内,A路线获得的最大值是19.B路线获得的最大值是21.
我的思路是:
景点有两个属性: 时间花费和满意价值,这个景点可以游玩也可以略过,由此很自然想到,属于01背包问题。
代码如下:
不知道哪里出问题了,就是提交不对!!请各位大哥指出!感激不尽。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 102
int v[M],t[M];
int mymap[M][M];
int n,m,T,s,e;
vector<int>q;//存储路径
int vis[M];
int maxvalue;
int dp[302];//背包
void solve(int cap)//01背包 求该路径上的最大满意价值
{
memset(dp,0,sizeof(dp));
for(int i=0;i<q.size();i++)
for(int time=cap;time>=t[ q[i] ];time--)
dp[time]=max(dp[time-t[ q[i] ]]+v[q[i]],dp[time]);
maxvalue=max(maxvalue,dp[cap]);
}
void myprint()
{
printf("*********\n");
for(int i=0;i<q.size();i++)
printf("%d ",q[i]);
printf("\n");
}
void dfs(int x,int time)
{
q.push_back(x);
vis[x]=1;//结点标记已访问
if(x==e)
{
//myprint();
solve(T-time);
return ;
}
for(int i=0;i<n;i++)
if(!vis[i]&&mymap[x][i]&&v[x]<v[i]&&mymap[x][i]+time<=T)
{
dfs(i,mymap[x][i]+time);
vis[i]=0;
q.pop_back();
}
}
int main()
{
int w,i,j,uu,vv,l,cases=1;
scanf("%d",&w);
while(w--)
{ // n景点数,m边数,T 是时间限制,s起点,e终点
scanf("%d%d%d%d%d",&n,&m,&T,&s,&e);
for(i=0;i<n;i++)//每个景点游玩需要的时间
scanf("%d",&t[i]);
for(i=0;i<n;i++)//每个景点获得的 满意价值
scanf("%d",&v[i]);
memset(mymap,0,sizeof(mymap));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&uu,&vv,&l);
if(mymap[uu][vv]==0||mymap[uu][vv]>l)
mymap[uu][vv]=mymap[vv][uu]=l;
}
maxvalue=0;//最大价值初始化为0
memset(vis,0,sizeof(vis));
q.clear();
dfs(s,0);
printf("Case #%d:\n%d\n",cases++,maxvalue);//输出最大的满意价值
}
return 0;
}
/*
1
4 4 22 0 3
1 1 1 1
5 7 9 12
0 1 10
0 2 10
1 3 10
2 3 10
*/