(Shortest + dp) 2020 cattle off winter training camp base algorithm 3J. Taurus treasure can dream Go

2020 cattle off winter training camp base algorithm 3J . Taurus treasure can dream Go

Ideas:

The shortest path is very simple, floyd you can find.
Then write the most common m 2 transfer. But T will fall. Simplification is to get, because the map is small, so after 200 steps can be moved to any position before the violent transfer of 200, before the extra 200 will use the prefix record.

Code:

#include<bits/stdc++.h>
#define pii pair<int,int>
#define int long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
const int N=1e6+10;
const int mod=1e7+9;
const int maxn=0x3f3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int maze[300][300];
struct mon
{
	int ti,id,w;
	int sum;
	mon()
	{
		sum=0;
	}
}a[N];
int n,m;
void floyd()
{
	int k,i,j;
	for(k=1;k<=n;k++)
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				maze[i][j]=min(maze[i][j],maze[i][k]+maze[k][j]);
	return;
}
int cmp(mon x,mon y)
{
	return x.ti<y.ti;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int i,j;
	cin>>n>>m;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			maze[i][j]=i==j?0:maxn;
	for(i=1;i<=m;i++)
	{
		int u,v;
		cin>>u>>v;
		maze[u][v]=1;
		maze[v][u]=1;
	}
	floyd();
	a[0].id=1;a[0].ti=0;a[0].w=0;
	int k;
	cin>>k;
	for(i=1;i<=k;i++)
		cin>>a[i].ti>>a[i].id>>a[i].w;
	sort(a,a+k+1,cmp);
	int res=0,premax;
	for(i=1;i<=k;i++)
	{
		if(i>200)
		{
			premax=max(premax,a[i-200].sum);
			a[i].sum=a[i].w+premax;
		}
		else
			a[i].sum=-maxn;
		for(j=1;j<=min(200LL,i);j++)
		{
			if(a[i].ti-a[i-j].ti>=maze[a[i].id][a[i-j].id])
				a[i].sum=max(a[i].sum,a[i-j].sum+a[i].w);
		}
		res=max(res,a[i].sum);
	}
	cout<<res<<endl;
	return 0;
}

Published 119 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/Z7784562/article/details/104231786