20分求助
查看原帖
20分求助
120855
清雅流水楼主2020/6/22 17:40
#include <bits/stdc++.h>
using namespace std ;
long long lx[25][25] ;
long long ky[25][105] ;
long long n , m , k , e , d ;
long long dp[105] ; //dp[a]:到a天花费 
struct node
{
	long long dh , lj ;
	bool operator < ( const node & a ) const
	{
		return lj > a.lj ;
	}
} wz , tmp ;
long long zdl( long long f , long long t )
{
	bool kz[25] ;
	long long lc[25] ;
	memset( lc , -1 , sizeof(lc) ) ;
	memset( kz , 0 , sizeof(kz) ) ;
	for( long long i = 0 ; i < m ; i ++ )
	{
		long long p ;
		if( f == 0 )
		{
			p = ky[i][t] ;
		}
		else
		{
			p = ky[i][t] - ky[i][f - 1] ;
		}
		if( p != 0 )
		{
//			cout<< i << ' ' ;
			kz[i] = 1 ;
		}
	}
//	cout<< endl ;
	priority_queue <node> q ;
	wz.dh = 0 ;
	wz.lj = 0 ;
	q.push( wz ) ;
	while( !q.empty() )
	{
		wz = q.top() ;
		q.pop() ;
		while( kz[wz.dh] == 1 && !q.empty() )
		{
			wz = q.top() ;
			q.pop() ;
		}
		if( kz[wz.dh] == 1 )
		{
			break ;
		}
		kz[wz.dh] = 1 ;
		lc[wz.dh] = wz.lj ;
//		cout<< wz.dh << endl ;
		for( long long i = 0 ; i < n ; i ++ )
		{
//			cout<< lx[wz.dh][i] << ' ' ;
			if( lx[wz.dh][i] != -1 && kz[i] == 0 )
			{
				tmp.dh = i ;
				tmp.lj = wz.lj + lx[wz.dh][i] ;
				q.push( tmp ) ;
			}
		}
//		cout<< endl ;
	}
//	cout<< f << ' ' << t << ' ' << lc[m - 1] << endl << endl ;
	return lc[m - 1] ;
}
int main ()
{
	memset( lx , -1 , sizeof(lx) ) ;
	long long t1 , t2 , t3 ;
	cin>> n >> m >> k >> e ;
	for( long long i = 0 ; i < e ; i ++ )
	{
		cin>> t1 >> t2 >> t3 ;
		t1 -- ;
		t2 -- ;
		if( lx[t1][t2] == -1 || lx[t1][t2] > t3 )
		{
//			cout<< 1 << endl ;
			lx[t1][t2] = lx[t2][t1] = t3 ;
		}
	}
	cin >> d ;
	for( long long i = 0 ; i < d ; i ++ )
	{
		cin >> t1 >> t2 >> t3 ;
		t1 -- ;
		t2 -- ;
		t3 -- ;
		for( long long j = t2 ; j <= t3 ; j ++ )
		{
			ky[t1][j] = 1 ;
		}
	}
	for( long long i = 1 ; i < n ; i ++ )
	{
		for( long long j = 0 ; j < m ; j ++ )
		{
			ky[j][i] += ky[j][i - 1] ;
		}
	}
	memset( dp , -1 , sizeof(dp) ) ;
	for( long long i = 0 ; i < n - 1 ; i ++ )
	{
		long long lc = zdl( 0 , i ) ;
		if( lc != -1 )
		if( dp[i] == -1 || dp[i] > ( lc * i + lc ) )
		{
			dp[i] = ( lc * i + lc ) ;
		}
		for( long long j = i + 1 ; j < n ; j ++ )
		{
			long long lc = zdl( i + 1 , j ) ;
//			cout<< lc << ' ' ;
			if( lc == -1 ) break ;
			if( dp[j] == -1 || dp[j] > ( lc * ( j - i ) + k + dp[i] ) )
			{
				dp[j] = ( lc * ( j - i ) + k + dp[i] ) ;
			}
		}
//		cout<< dp[i] << endl ;
//		cout<< endl ;
	}
	cout<< dp[n - 1] ;
	return 0 ;
}
2020/6/22 17:40
加载中...