问题 J: Treasure Map
时间限制: 1 Sec 内存限制: 128 MB提交: 126 解决: 24
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
You have found a treasure map! The map leads you to several gold mines. The mines
each produce gold each day, but the amount of gold that they produce diminishes each day. There are paths between the mines. It may take several days to go from one mine to another. You can collect all of the day’s gold from a mine when you are there, but you have to move on, you cannot stay for multiple days at the same mine. However, you can return to a mine after leaving it.
输入
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with a line containing two integers n (2 ≤ n ≤ 1,000) and m (1 ≤ m ≤ 1,000), where n is the number of mines, and m is the number of paths.
The next n lines will each describe a mine with two integers, g (1 ≤ g ≤ 1,000) and d (1 ≤ d ≤ 1,000), where g is the amount of gold mined on day 1, and d is the amount by which the gold haul diminishes each day. For example, if g=9 and d=4, then on day 1, the mine produces 9, on day 2 it produces 5, on day 3 it produces 1, and from day 4 on, it produces 0 (the mines cannot produce negative amounts of gold). The mines are numbered 1..n in the order that they appear in the input, and you start at mine 1 on day 1.
The next m lines will each describe a path with three integers, a, b (1 ≤ a < b ≤ n) and t (1 ≤ t ≤ 100), where the path goes from mine a to mine b, and takes t days to traverse. The paths go in both directions, so that a path that goes from a to b can also be used to go from b to a.
The next n lines will each describe a mine with two integers, g (1 ≤ g ≤ 1,000) and d (1 ≤ d ≤ 1,000), where g is the amount of gold mined on day 1, and d is the amount by which the gold haul diminishes each day. For example, if g=9 and d=4, then on day 1, the mine produces 9, on day 2 it produces 5, on day 3 it produces 1, and from day 4 on, it produces 0 (the mines cannot produce negative amounts of gold). The mines are numbered 1..n in the order that they appear in the input, and you start at mine 1 on day 1.
The next m lines will each describe a path with three integers, a, b (1 ≤ a < b ≤ n) and t (1 ≤ t ≤ 100), where the path goes from mine a to mine b, and takes t days to traverse. The paths go in both directions, so that a path that goes from a to b can also be used to go from b to a.
输出
Output a single integer, which is the maximum amount of gold that you can collect.
样例输入
2 1
10 1
10 2
1 2 1
样例输出
42
考点:思维题
#include<bits/stdc++.h>
using namespace std;
#define maxn 2005
#define inf 0x3f3f3f3f
int n,k;
char pic[maxn][maxn];
int hor[maxn],ver[maxn],d[maxn][maxn];
//hor代表到当前点为止第i行的到此处的最优位置(ver则代表列)。
int main()
{
while(cin>>n>>k)
{
for(int i=0; i<n; i++)
scanf("%s",pic[i]);
memset(hor,0,sizeof(hor));
memset(ver,0,sizeof(ver));
memset(d,inf,sizeof(d));
d[0][0]=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++){
if(pic[i][j]=='#') continue;
for(; hor[i]<j; hor[i]++)
if((j-hor[i])<=k&&pic[i][hor[i]]=='.') break;
d[i][j]=min(d[i][j],d[i][hor[i]]+1);
for(; ver[j]<i; ver[j]++)
if((i-ver[j])<=k&&pic[ver[j]][j]=='.') break;
d[i][j]=min(d[i][j],d[ver[j]][j]+1);
if(d[i][j]<=d[i][hor[i]]) hor[i]=j;
if(d[i][j]<=d[ver[j]][j]) ver[j]=i;
}
if(d[n-1][n-1]==inf) puts("-1");
else printf("%d\n",d[n-1][n-1]);
}
return 0;
}