求最短路:把一个集合的点看做是一个点,这样就可以用djstra算法做了。然后由于每个点最多标记一次最短路,用set维护一个点集合。
当最短路找到一个一个集合的时候,把这个集合里还存在的点都取出即可。取出后,每个点又可以去两个集合,
再向保存最短路的set里更新集合信息即可。详细看代码。
#include
#include
#include
#include
#include
using namespace std;
#define maxn 200007
#define ll long long
int lp[maxn],rp[maxn];
ll cosw[maxn];
ll dist[maxn];
set haha;
struct Node{
int id;
ll cost;
};
bool operator < (Node a,Node b){
if(a.cost == b.cost) return a.id < b.id;
return a.cost < b.cost;
}
set mind;
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 0;i < n; i++)
scanf("%d",&lp[i]);
for(int i = 0;i < n; i++)
scanf("%d",&rp[i]);
for(int i = 0;i < n; i++)
scanf("%d",&cosw[i]);
haha.clear();
mind.clear();
memset(dist,-1,sizeof(dist));
dist[0] = 0;
Node x,y;
x.id = 0;
x.cost = cosw[0];
mind.insert(x);
for(int i = 1;i < n; i++)
haha.insert(i);
set::iterator it,it2;
while(mind.size() > 0){
x = *mind.begin();
mind.erase(mind.begin());
it = haha.lower_bound(x.id - rp[x.id]);
while(it != haha.end() && *it <= x.id - lp[x.id]){
y.id = *it;
y.cost = x.cost + cosw[y.id];
dist[y.id] = x.cost;
mind.insert(y);
it2 = it++;
haha.erase(it2);
}
it = haha.lower_bound(x.id + lp[x.id]);
while(it != haha.end() && *it <= x.id + rp[x.id]){
y.id = *it;
y.cost = x.cost + cosw[y.id];
dist[y.id] = x.cost;
mind.insert(y);
it2 = it++;
haha.erase(it2);
}
}
for(int i = 0;i < n; i++){
if(i) printf(" ");
printf("%I64d",dist[i]);
}
printf("\n");
}
return 0;
}