codevs1391 伊吹萃香

来源:互联网 发布:php空间试用 编辑:程序博客网 时间:2024/06/18 00:26

题目描述 Description
在幻想乡,伊吹萃香是能够控制物体密度的鬼王。因为能够控制密度,所以萃香能够制造白洞和黑洞,并可以随时改变它们。某一天萃香闲着无聊,在妖怪之山上设置了一些白洞或黑洞,由于引力的影响,给妖怪们带来了很大的麻烦。于是他们决定找出一条消耗体力最少的路,来方便进出。已知妖怪之山上有N个路口(编号1..N),每个路口都被萃香设置了一定质量白洞或者黑洞。原本在各个路口之间有M条单向路,走过每一条路需要消耗一定量的体力以及1个单位的时间。由于白洞和黑洞的存在,走过每条路需要消耗的体力也就产生了变化,假设一条道路两端路口黑白洞的质量差为delta:

  1. 从有白洞的路口走向有黑洞的路口,消耗的体力值减少delta,若该条路径消耗的体力值变为负数的话,取为0。

  2. 从有黑洞的路口走向有白洞的路口,消耗的体力值增加delta。

  3. 如果路口两端均为白洞或黑洞,消耗的体力值无变化。

由于光是放置黑洞白洞不足以体现萃香的强大,所以她决定每过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的路线。

这个题目就是用spfa跑分层图。。分等待和不等待。只要可以更新就进行更新,最后取一个min。

上代码:

#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>#include<cstring>using namespace std;const int maxn=60005;       //0->white 1->black struct dqs{    int f,t,c;}hh[maxn];struct dqm{    int pos;    bool bw;};int first[maxn],next[maxn];int tot=0;void build(int f,int t,int c){    hh[++tot]=(dqs){f,t,c};    next[tot]=first[f];    first[f]=tot;}bool color[maxn],used[maxn][2];int dis[maxn][2],w[maxn],s[maxn];int cha(int f,int t,int bow){    int fcha=color[f]^bow;    int tcha=color[f]^bow;    if(fcha==tcha)        return 0;    else    {        int cha=abs(w[f]-w[t]);        if(fcha==1&&tcha==0)            return cha;        else            return -cha;    }}deque<dqm>q;void spfa(int st){    dqm begin;    begin.pos=st;    begin.bw=0;    q.push_back(begin);    dis[st][0]=0;    used[st][0]=1;    while(!q.empty())    {    //  cout<<"233333333"<<endl;        dqm head=q.front();        q.pop_front();        used[head.pos][head.bw]=0;        for(int i=first[head.pos];i;i=next[i])        {            int u=hh[i].t;            dqm now;            now.pos=u;            now.bw=head.bw^1;                       if(dis[u][now.bw]>dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0))            {                dis[u][now.bw]=dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0);                if(!used[u][now.bw])                {                     if(dis[u][now.bw]<=dis[q.front().pos][q.front().bw])                        q.push_front(now);                    else                        q.push_back(now);                    used[u][now.bw]=1;                                  }            }           }        if(dis[head.pos][head.bw^1]>dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw))        {            dis[head.pos][head.bw^1]=dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw);            if(!used[head.pos][head.bw^1])            {                used[head.pos][head.bw^1]=1;                                if(dis[head.pos][head.bw^1]<=dis[q.front().pos][q.front().bw])                    q.push_front((dqm){head.pos,head.bw^1});                else                    q.push_back((dqm){head.pos,head.bw^1});             }        }    }}int main(){    int n,m,f,t,c;    scanf("%d%d",&n,&m);    memset(dis,0x3f,sizeof(dis));    for(int i=1;i<=n;i++)        scanf("%d",&color[i]);    for(int i=1;i<=n;i++)        scanf("%d",&w[i]);    for(int i=1;i<=n;i++)        scanf("%d",&s[i]);    for(int i=1;i<=m;i++)    {        scanf("%d%d%d",&f,&t,&c);        build(f,t,c);    }    spfa(1);    printf("%d",min(dis[n][0],dis[n][1]));    return 0;}

这个代码是有bug的,在本机测试会RE,但是能AC。。。不得不说codevs数据实在是太水了。。。这里RE的原因是我没有判断它在spfa的过程中队头是不是空!!!

附正确代码(不加slf优化):

#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>#include<cstring>using namespace std;const int maxn=60005;       //0->white 1->black struct dqs{    int f,t,c;}hh[maxn];struct dqm{    int pos;    bool bw;};int first[maxn],next[maxn];int tot=0;void build(int f,int t,int c){    hh[++tot]=(dqs){f,t,c};    next[tot]=first[f];    first[f]=tot;}bool color[maxn],used[maxn][2];int dis[maxn][2],w[maxn],s[maxn];int cha(int f,int t,int bow){    int fcha=color[f]^bow;    int tcha=color[t]^bow;    if(fcha==tcha)        return 0;    else    {        int cha=abs(w[f]-w[t]);        if(fcha==1&&tcha==0)            return cha;        else            return -cha;    }}queue<dqm>q;void spfa(int st){    dqm begin;    begin.pos=st;    begin.bw=0;    q.push(begin);    dis[st][0]=0;    used[st][0]=1;    while(!q.empty())    {        dqm head=q.front();        q.pop();        used[head.pos][head.bw]=0;        for(int i=first[head.pos];i;i=next[i])        {            int u=hh[i].t;            dqm now;            now.pos=u;            now.bw=head.bw^1;                       if(dis[u][now.bw]>dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0))            {                dis[u][now.bw]=dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0);                if(!used[u][now.bw])                {                    q.push(now);                    used[u][now.bw] = true;                                 }            }           }        if(dis[head.pos][head.bw^1]>dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw))        {            dis[head.pos][head.bw^1]=dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw);            if(!used[head.pos][head.bw^1])            {                used[head.pos][head.bw^1]=1;                                q.push((dqm){head.pos,head.bw^1});              }        }    }}int main(){    int n,m,f,t,c;    scanf("%d%d",&n,&m);    memset(dis,0x3f,sizeof(dis));    for(int i=1;i<=n;i++)        scanf("%d",&color[i]);    for(int i=1;i<=n;i++)        scanf("%d",&w[i]);    for(int i=1;i<=n;i++)        scanf("%d",&s[i]);    for(int i=1;i<=m;i++)    {        scanf("%d%d%d",&f,&t,&c);        build(f,t,c);    }    spfa(1);    printf("%d",min(dis[n][0],dis[n][1]));    return 0;}

加入slf优化:

#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>#include<cstring>using namespace std;const int maxn=60005;       //0->white 1->black struct dqs{    int f,t,c;}hh[maxn];struct dqm{    int pos;    bool bw;};int first[maxn],next[maxn];int tot=0;void build(int f,int t,int c){    hh[++tot]=(dqs){f,t,c};    next[tot]=first[f];    first[f]=tot;}bool color[maxn],used[maxn][2];int dis[maxn][2],w[maxn],s[maxn];int cha(int f,int t,int bow){    int fcha=color[f]^bow;    int tcha=color[t]^bow;    if(fcha==tcha)        return 0;    else    {        int cha=abs(w[f]-w[t]);        if(fcha==1&&tcha==0)            return cha;        else            return -cha;    }}deque<dqm>q;void spfa(int st){    dqm begin;    begin.pos=st;    begin.bw=0;    q.push_back(begin);    dis[st][0]=0;    used[st][0]=1;    while(!q.empty())    {        dqm head=q.front();        q.pop_front();        used[head.pos][head.bw]=0;        for(int i=first[head.pos];i;i=next[i])        {            int u=hh[i].t;            dqm now;            now.pos=u;            now.bw=head.bw^1;                       if(dis[u][now.bw]>dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0))            {                dis[u][now.bw]=dis[head.pos][head.bw]+max(hh[i].c+cha(head.pos,u,head.bw),0);                if(!used[u][now.bw])                {                    if(q.size()!=0)                    {                         if(dis[u][now.bw]<=dis[q.front().pos][q.front().bw])                            q.push_front(now);                        else                            q.push_back(now);                               }                    else                        q.push_back(now);                    used[u][now.bw]=1;                                  }            }           }        if(dis[head.pos][head.bw^1]>dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw))        {            dis[head.pos][head.bw^1]=dis[head.pos][head.bw]+s[head.pos]*(color[head.pos]^head.bw);            if(!used[head.pos][head.bw^1])            {                used[head.pos][head.bw^1]=1;                if(q.size()!=0)                {                    if(dis[head.pos][head.bw^1]<=dis[q.front().pos][q.front().bw])                        q.push_front((dqm){head.pos,head.bw^1});                    else                        q.push_back((dqm){head.pos,head.bw^1});                                                         }                           else                    q.push_back((dqm){head.pos,head.bw^1});             }        }    }}int main(){    int n,m,f,t,c;    scanf("%d%d",&n,&m);    memset(dis,0x3f,sizeof(dis));    for(int i=1;i<=n;i++)        scanf("%d",&color[i]);    for(int i=1;i<=n;i++)        scanf("%d",&w[i]);    for(int i=1;i<=n;i++)        scanf("%d",&s[i]);    for(int i=1;i<=m;i++)    {        scanf("%d%d%d",&f,&t,&c);        build(f,t,c);    }    spfa(1);    printf("%d",min(dis[n][0],dis[n][1]));    return 0;}
5 0