码迷,mamicode.com
首页 > 其他好文 > 详细

luogu_P1993 小K的农场

时间:2019-10-18 10:54:33      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:blank   cost   open   gif   lap   for   mes   spl   tps   

---恢复内容开始---

传送门:https://www.luogu.org/problem/P1993

查分约束:

对于条件1:d[a]-d[b]>=c

       d[a]>=d[b]+c

对于条件2:d[a]-d[b]<=c

          d[b]>=d[a]-c

对于条件3:d[a]>=d[b]

          d[b]>=d[a]

愉快的建边,我的跑最长路,您也可以跑最短路。

本人sq把add写错了TAT

还有就是这道题卡广搜spfa

需要用深搜来实现,所以看(chao)了一下题解的写法......不能说明什么,只能说明我太弱了。

技术图片
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define R register
using namespace std;
int n,m,head[12000],tot,ver[41000],nex[41000],cost[41000];
inline void add(int x,int y,int z){
    ver[++tot]=y;nex[tot]=head[x];cost[tot]=z;head[x]=tot;
}
bool v[12000];
int d[12000],cnt[12000];
inline bool spfa(int x){
        v[x]=1;
        for(R int i=head[x];i;i=nex[i]){
            if(d[ver[i]]<d[x]+cost[i]){
                d[ver[i]]=d[x]+cost[i];
                if(v[ver[i]]) return 0;
                if(!spfa(ver[i])) return 0;
            }
        }
    v[x]=0;
    return 1;
}
int main (){
    scanf("%d%d",&n,&m);
    for(R int op,x,y,z,i=1;i<=m;i++){
        scanf("%d%d%d",&op,&x,&y);
        // longest
        if(op==1){
            scanf("%d",&z);
            add(y,x,z);
        }
        else if(op==2){
            scanf("%d",&z);
            add(x,y,-z);
        }
        else if(op==3){
            add(x,y,0);add(y,x,0);
        }
    }
    for(R int i=1;i<=n;i++){
        add(0,i,0);d[i]=-1e9;
    }
    if(spfa(0)) printf("Yes\n");
    else printf("No\n");
    return 0;
}
View Code

 

luogu_P1993 小K的农场

标签:blank   cost   open   gif   lap   for   mes   spl   tps   

原文地址:https://www.cnblogs.com/coclhy/p/11697010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!