【BZOJ1003】[ZJOI2006]物流运输trans【最短路】【DP】

被水淹没,不知所措。


/* Footprints In The Blood Soaked Snow */
#include 
#include 

using namespace std;

const int maxn = 25, maxm = 405, maxq = 10000, inf = 0x3f3f3f3f;

int n, m, k, t, head[maxn], cnt, q[maxq], id[maxm], st[maxm], ed[maxm], dp[maxm], dis[maxn];
bool vis[maxn];

struct _edge {
	int v, w, next;
} g[maxm << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int add(int u, int v, int w) {
	g[cnt] = (_edge) {v, w, head[u]};
	head[u] = cnt++;
}

inline void spfa(int d1, int d2) {
	for(int i = 1; i <= n; i++) vis[i] = 0, dis[i] = inf;
	for(int i = 1; i <= t; i++) if(!(ed[i] < d1 || st[i] > d2)) vis[id[i]] = 1;
	int h = 0, t = 0, u, i; dis[q[t++] = 1] = 0;
	while(h != t) for(i = head[u = q[h++]]; ~i; i = g[i].next) if(!vis[g[i].v] && dis[g[i].v] > dis[u] + g[i].w) {
		dis[g[i].v] = dis[u] + g[i].w;
		q[t++] = g[i].v;
	}
}

int main() {
	m = iread(); n = iread(); k = iread();
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;
	
	int e = iread();
	while(e--) {
		int x = iread(), y = iread(), c = iread();
		add(x, y, c); add(y, x, c);
	}

	t = iread();
	for(int i = 1; i <= t; i++)
		id[i] = iread(), st[i] = iread(), ed[i] = iread();

	dp[0] = 0;
	for(int i = 1; i <= m; i++) {
		dp[i] = inf;
		for(int j = 0; j < i; j++) {
			spfa(j + 1, i);
			if(dis[n] == inf) continue;
			else dp[i] = min(dp[i], dp[j] + (i - j) * dis[n] + k);
		}
	}

	printf("%d\n", dp[m] - k);
	return 0;
}


你可能感兴趣的:(普通DP,最短路)