zoukankan      html  css  js  c++  java
  • POJ 3169 Layout 差分约束系统

    d_x <= d_(x+1)  --> d_x <= d_(x+1) + 0 

    d_y - d_x <= maxd --> d_y <= d_x +maxd

    d_y - d_x >= mind --> d_x <= d_y - mind 

    -----------

    const int maxn=4100;
    const int maxm=180000;
    int n;
    struct EdgeNode{
        int to;
        int w;
        int next;
    };
    struct BellmanFord{
        EdgeNode edges[maxm];
        int head[maxn],edge,n;
        bool vis[maxn];
        int outque[maxn];
        queue<int>que;
        int dis[maxn];
        void addedge(int u,int v,int c){
            edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        }
        void init(int n){
            memset(head,-1,sizeof(head));
            edge=0;
            this->n=n;
        }
        bool spfa(int s){
            int u;
            for (int i=0;i<=n;i++) dis[i]=INF;
            memset(vis,0,sizeof(vis));
            memset(outque,0,sizeof(outque));
            while (!que.empty()) que.pop();
            que.push(s);
            vis[s]=true;
            dis[s]=0;
            while (!que.empty()){
                u=que.front();
                que.pop();
                vis[u]=false;
                outque[u]++;
                if (outque[u]>n) return false;
                for (int i=head[u];i!=-1;i=edges[i].next){
                    int v=edges[i].to;
                    int w=edges[i].w;
                    if (dis[v]==INF||dis[v]>dis[u]+w){
                        dis[v]=dis[u]+w;
                        if (!vis[v]){
                            vis[v]=true;
                            que.push(v);
                        }
                    }
                }
            }
            return true;
        }
    }g;
    int ml,md;
    int main(){
        while (~scanf("%d%d%d",&n,&ml,&md)){
            g.init(n);
            for (int i=1;i<n;i++){
                g.addedge(i+1,i,0);
            }
            for (int i=0;i<ml;i++){
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                g.addedge(x,y,z);
            }
            for (int i=0;i<md;i++){
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                g.addedge(y,x,-z);
            }
            if (g.spfa(1)){
                if (g.dis[n]==INF) printf("-2
    ");
                else printf("%d
    ",g.dis[n]);
            }
            else{
                printf("-1
    ");
            }
        }
    	return 0;
    }
    


    -----------

  • 相关阅读:
    从零搭建ES搜索服务(一)基本概念及环境搭建
    SpringBoot+Mybatis多模块(module)项目搭建教程
    Redis分布式锁实现方式(附有正解及错误示例)
    MySQL QA
    Netty handler处理类无法使用@Autowired注入bean的解决方法
    数组的全排列
    链表分段反转
    tomcat调优
    Spring Boot之JdbcTemplate多数据源配置与使用
    aPaaS
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681535.html
Copyright © 2011-2022 走看看