##链接##
题目描述 Description
在幻想乡,伊吹萃香是能够控制物体密度的鬼王。因为能够控制密度,所以萃香能够制造白洞和黑洞,并可以随时改变它们。某一天萃香闲着无聊,在妖怪之山上设置了一些白洞或黑洞,由于引力的影响,给妖怪们带来了很大的麻烦。于是他们决定找出一条消耗体力最少的路,来方便进出。已知妖怪之山上有N个路口(编号1…N),每个路口都被萃香设置了一定质量白洞或者黑洞。原本在各个路口之间有M条单向路,走过每一条路需要消耗一定量的体力以及1个单位的时间。由于白洞和黑洞的存在,走过每条路需要消耗的体力也就产生了变化,假设一条道路两端路口黑白洞的质量差为delta:
-
从有白洞的路口走向有黑洞的路口,消耗的体力值减少delta,若该条路径消耗的体力值变为负数的话,取为0。
-
从有黑洞的路口走向有白洞的路口,消耗的体力值增加delta。
-
如果路口两端均为白洞或黑洞,消耗的体力值无变化。
由于光是放置黑洞白洞不足以体现萃香的强大,所以她决定每过1个单位时间,就把所有路口的白洞改成黑洞,黑洞改成白洞。当然在走的过程中你可以选择在一个路口上停留1个单位的时间,如果当前路口为白洞,则不消耗体力,否则消耗s[i]的体力。现在请你计算从路口1走到路口N最小的体力消耗。保证一定存在道路从路口1到路口N。
输入描述 Input Description
第1行:2个正整数N, M
第2行:N个整数,第i个数为0表示第i个路口开始时为白洞,1表示黑洞
第3行:N个整数,第i个数表示第i个路口设置的白洞或黑洞的质量w[i]
第4行:N个整数,第i个数表示在第i个路口停留消耗的体力s[i]
第5…M+4行:每行3个整数,u, v, k,表示在没有影响的情况下,从路口u走到路口v需要消耗k的体力。
输出描述 Output Description
第1行:1个整数,表示消耗的最小体力
样例输入 Sample Input
4 5
1 0 1 0
10 10 100 10
5 20 15 10
1 2 30
2 3 40
1 3 20
1 4 200
3 4 200
样例输出 Sample Output
130
数据范围及提示 Data Size & Hint
对于30%的数据:1 <= N <= 100, 1 <= M <= 500
对于60%的数据:1 <= N <= 1,000, 1 <= M <= 5,000
对于100%的数据:1 <= N <= 5,000, 1 <= M <= 30,000
其中20%的数据为1 <= N <= 3000的链
1 <= u,v <= N, 1 <= k,w[i],s[i] <= 200
按照1 -> 3 -> 4的路线。
考建图
1——n为黑点,n+1——n+n为白点
如果连接的两点的初始颜色相同则连(u,v+n)(u+n,v)
如果不同则连(u,v)(u+n,v+n)
最后根据1的颜色跑spfa,输出d[n],d[n+n]的最大值
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 100000 + 100;
const int inf = 1<<29;
int n,m,col[maxn],val[maxn],s[maxn];
struct edge {
int u,v,w;
int next;
}e[maxn*2];
int head[maxn],tot = 0;
int read() {
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void add(int u, int v, int w) {
e[++tot] = (edge){u,v,w,head[u]};
head[u] = tot;
}
int vis[maxn],d[maxn];
void spfa(int x) {
queue<int>q;
for(int i = 1; i <= n + n; i++) d[i] = inf;
q.push(x);
vis[x] = 1, d[x] = 0;
while(!q.empty()) {
int k = q.front();
q.pop();
vis[k] = 0;
for(int i = head[k]; i; i = e[i].next) {
int v = e[i].v;
if(d[v] > d[k] + e[i].w) {
d[v] = d[k] + e[i].w;
if(!vis[v]) {
q.push(v);
vis[v] = 1;
}
}
}
}
}
int main() {
n = read(), m = read();
for(int i = 1; i <= n; i++) col[i] = read();
for(int i = 1; i <= n; i++) val[i] = read();
for(int i = 1; i <= n; i++) s[i] = read();
for(int i = 1; i <= m; i++) {
int u = read(), v = read(), w = read();
if(col[u] == col[v]) {
add(u,v+n,w);
add(u+n,v,w);
}
else {
int t = abs(val[u] - val[v]);
add(u,v,w + t);
add(u+n,v+n,max(0,w - t));
}
}
for(int i = 1; i <= n; i++) {
add(i,i+n,s[i]);
add(i+n,i,0);
}
if(col[1]) spfa(1);
else spfa(1+n);
int p = max(d[n],d[n+n]);
cout<<p<<endl;
return 0;
}