zoukankan      html  css  js  c++  java
  • 洛谷1260 工程规划

    传送门

    差分约束裸题。

    Debug超级久到怀疑人生,最后发现读入优化写错了系列。

    //Twenty
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<queue>
    typedef long long LL;
    const int maxn=1005;
    const int maxm=10005;
    using namespace std;
    int n,m,l,r,w;
    
    namespace fastIO {
       const int sz=1<<15|1;
       char ch,buf[sz],*l,*r;
       void gechar(char &c) {
           if(l==r) r=(l=buf)+fread(buf,1,sz,stdin);
    	   c = l==r?(char)EOF:*l++; 
       }
       template<typename T> void read(T &x) {
           int f=1; x=0; gechar(ch);
           while(ch!='-'&&(ch<'0'||ch>'9')) gechar(ch);
           if(ch=='-') f=-1,gechar(ch);
           for(;ch>='0'&&ch<='9';gechar(ch)) x=x*10+ch-'0'; x=x*f;
       }
    }
    
    int ecnt,fir[maxn],nxt[maxm],to[maxm],val[maxm],cnt[maxn];
    void add(int u,int v,int w) {
        nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
    }
    
    void init() {
    	fastIO::read(n);
    	fastIO::read(m);
    	for(int i=1;i<=n;i++) 
    	    add(0,i,0);
    	for(int i=1;i<=m;i++) {
    		fastIO::read(l);
    		fastIO::read(r);
    		fastIO::read(w);
    		add(l,r,w);
        }
    }
    
    int vis[maxn],dis[maxn],fl;
    queue<int>que;
    int spfa(int s) {
        memset(dis,127,sizeof(dis));
        dis[s]=0;
        que.push(s); 
        while(!que.empty()) {
            int x=que.front();
            que.pop();
            vis[x]=0;
            for(int i=fir[x];i;i=nxt[i]) {
                if(dis[to[i]]>dis[x]+val[i]) {
                    dis[to[i]]=dis[x]+val[i];
                    if(!vis[to[i]]) {
                        vis[to[i]]=1;
                        ++cnt[to[i]];
                        if(cnt[to[i]]==n) {
                        	fl=1;
    						return 0;	
                        }
                        que.push(to[i]); 
                    }
                }
            }
        }
    }
    
    void work() {
        spfa(0);
        if(fl) {
    	    printf("NO SOLUTION
    ");
    	    return;
    	}
    	for(int i=1;i<=n;i++)
    	    printf("%d
    ",-dis[i]);
    }
    
    //#define DEBUG
    int main()
    {
    #ifdef DEBUG
        freopen("1.in","r",stdin);
    #endif 
        init();
        work();
        return 0;
    }
    

      

  • 相关阅读:
    高级程序员应该具备什么能力
    二 八 定 律
    二八定律全面分析
    Java 网络爬虫获取网页源代码原理及实现
    vijosP1371 方程的解
    vijosP1413 Valentine’s Present
    vijosP1289 老板娘的促销方案
    vijosP1092 全排列
    vijosP1049 送给圣诞夜的礼品
    vijosP1210 盒子与球
  • 原文地址:https://www.cnblogs.com/Achenchen/p/7732364.html
Copyright © 2011-2022 走看看