Southeast USA ICPC 2017 | GYM -101617 J:Treasure Map (DP or BFS)

Treasure Map

时间限制: 1 Sec  内存限制: 128 MB
提交: 128  解决: 25
[提交][状态][讨论版][命题人: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. 

输出

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

提示

来源


[思路]

 在codeforces 上  BFS  不会超时,  在 UPC 上BFS 稳稳超时,   队友的 n^2  竟然不会超时,

  对不起队友, 训练的时候, 队友要写他那个 n^2 dp  我斩钉截铁的认为一定会超时,  就一直写我的bfs

    打脸啊,

BFS 思路的话, 从 点 1 开始  松弛 有关系的节点, 然后 一直bfs 下去, 维护 最大值, 


[code]

CF 上 不会超时

#include <iostream>
#include <bits/stdc++.h>
 
typedef long long ll;
using namespace std;
const int MAXN = 1e6+ 7;
const int N=1e3+10;
const int INF = 1e9 + 7;
 
ll head[MAXN],dp[N][N];
ll a[N][2];
ll n,m;
ll ans;
ll cot;
struct Node{
    ll u,v,w,next;
}edge[MAXN<<1];
struct node{
	ll val,day,id;
	bool operator<(const node& pre)const{
		return day > pre.day;
	}
};
void init()
{
    cot = 0;
    memset(head,-1,sizeof(head));
}
void add(ll u,ll v,ll w)
{
    edge[++cot].v = v;
    edge[cot].w = w;
    edge[cot].next = head[u];
    head[u] = cot;
}
ll Value(ll j,ll n)
{
    return a[j][0] - (n-1)*a[j][1];
}
void solve()
{
 	memset(dp,0,sizeof(dp));
    priority_queue<node>Q;
    node p;
	p.day = 1;
	p.id = 1;
	p.val= a[1][0];
    Q.push(p);
   	ans = a[1][0];
    dp[1][1] = a[1][0];
    while(!Q.empty())
    {
        node pre=Q.top();
        Q.pop();
        int u = pre.id;
        for(int i = head[u];i!=-1;i = edge[i].next)
        {
            ll e = edge[i].v;// next e
            ll w = edge[i].w;//next_
			
			node temp = pre;
			temp.id = e;
            ll val = Value(e,pre.day+w);
           
			temp.val += val > 0 ? val:0;
			//cout<<temp.val<<endl;
			temp.day= pre.day+w;
		//	cout<<dp[e][temp.day]<<" "<<temp.day<<endl;
			if(temp.day > 10000)
				return ;
			if( temp.val <= dp[e][temp.day]) 
				continue;            
            dp[e][temp.day] =  temp.val ;
            ans = max(ans,temp.val);
            Q.push(temp);
        }
    }

}
/*
3 2
10 1
10 2
20 1
1 2 1
2 3 1
*/

int main()
{
    init();
    scanf("%I64d %I64d",&n,&m);
    memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d %I64d",&a[i][0],&a[i][1]);
    }
    for(int i=1;i<=m;i++)
    {
        ll x,y,z;
        scanf("%I64d %I64d %I64d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    solve();
    printf("%I64d\n",ans);
    return 0;
}


DP

#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
const int N=1e3+10;
struct Node{
    int to,time;
};
 
vector<Node>edge[N];
ll dp[N][N];
int g[N],d[N];
int main()
{
    memset(dp,0,sizeof(dp));
    int n,m;
    int u,v,t;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&g[i],&d[i]);
    while(m--)
    {
        scanf("%d%d%d",&u,&v,&t);
        Node p;
        p.time=t;
        p.to=v;
        edge[u].push_back(p);
        p.to=u;
        edge[v].push_back(p);
    }
    dp[1][1]=g[1];
    for(int i=1;i<=1001;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=0;k<edge[j].size();k++)
            {
                int to=edge[j][k].to;
                int time=edge[j][k].time;
                if(i-time>=1&&dp[i-time][to])
                    dp[i][j]=max(dp[i][j],dp[i-time][to]+max(0,g[j]-(i-1)*d[j]));
            }
          //  printf("%d,%d %d\n",i,j,dp[i][j]);
        }
    }
    ll ans=0;
    for(int i=0;i<=1001;i++)
        for(int j=1;j<=n;j++)
            ans=max(ans,dp[i][j]);
    printf("%lld\n",ans);
    return 0;
}

123

posted @ 2018-04-29 13:11  Sizaif  阅读(238)  评论(0编辑  收藏  举报