//差分约束  >=求最长路径 <=求最短路径   结果都一样
//spfa
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<queue>
using namespace std;
#define N 1010
#define M 1010*1010//注意边和点集的数组大小
struct edge
{
int to,value,next;
};
struct edge edges[M];
int heads[N],len=;
int dis[N];
int addedge(int u,int v,int w)
{
edges[len].to=v,edges[len].value=w,edges[len].next=heads[u];
heads[u]=len++;
return ;
}
int n,m; int spfa(int v)
{
queue<int> q;
int inqueue[N];
memset(inqueue,,sizeof(inqueue)),inqueue[v]=;
q.push(v);
for(int i=;i<=n;i++) dis[i]=INT_MIN;
dis[v]=;
int times[N];
memset(times,,sizeof(times)),times[v]=;
int temp=;
while(!q.empty()){
int x=q.front();
q.pop();
inqueue[x]=; for(int i=heads[x];i!=-;i=edges[i].next){
int to=edges[i].to,value=edges[i].value;
if(value+dis[x]>dis[to]){
dis[to]=value+dis[x]; if(!inqueue[to]){ //注意已经在队列里面的不用再加入队列
inqueue[to]=,q.push(to);
times[to]++;
if(times[x]>n){
return -;//返回值有可能是0 不能将0作为区别的标记 } }
}
}
}
return dis[n];
}
int main(void)
{ int i,num;
while(scanf("%d%d",&n,&m)!=EOF){
len=;
memset(heads,-,sizeof(heads));
for(i=;i<=n;i++){
scanf("%d",&num);
addedge(i,i-,-num);
addedge(,i,);
}
for(i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u-,v,w);
} int h=spfa();
if(h!=-) printf("%d\n",h);
else printf("Bad Estimations\n"); }
return ;
}
//bellman
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
#define INF 2000000000
int u[12005],v[12005],w[12005],n,m,total,d[1005];
void Edge(int s,int e,int val){
u[total]=s;
v[total]=e;
w[total++]=val;
}
int Bellman(){
int i,e;
for(i=1;i<=n;i++) d[i]=-INF;
d[0]=0;
for(i=1;i<=n;i++){
for(e=0;e<total;e++){
if(d[v[e]]<d[u[e]]+w[e])
d[v[e]]=d[u[e]]+w[e];
}
}
int ans=d[n];
for(e=0;e<total;e++)
if(d[v[e]]<d[u[e]]+w[e]) ans=-1;
return ans;
}
int main(){
int a,b,c,cnt,i;
while(scanf("%d%d",&n,&m)!=EOF){
total=0;
for(i=1;i<=n;i++){
scanf("%d",&c);
Edge(0,i,0);
Edge(i,i-1,-c);
}
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
Edge(a-1,b,c);
} cnt=Bellman();
for(i=0;i<n;i++) printf("%d ",d[i]);
puts("");
if(cnt==-1) cout<<"Bad Estimations\n";
else cout<<cnt<<"\n";
}
return 0;
}

  

 

ZOJ 2770 Burn the Linked Camp(spfa&&bellman)的更多相关文章

  1. ZOJ 2770 Burn the Linked Camp 差分约束

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...

  2. ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...

  3. zoj 2770 Burn the Linked Camp

    今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...

  4. zoj 2770 Burn the Linked Camp (差分约束系统)

    // 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...

  5. Burn the Linked Camp(bellman 差分约束系统)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  6. zoj Burn the Linked Camp (查分约束)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  7. ZOJ2770 Burn the Linked Camp(差分约束系统)

    区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...

  8. zoj2770 Burn the Linked Camp --- 差分约束

    有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...

  9. ZOJ 2770 差分约束+SPFA

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

随机推荐

  1. Umbraco扩展开发

    国内Umbraco方面的资料很少,搜集到一些国外的优秀项目或插件.记录下来,便于日后使用: backoffice:https://github.com/TimGeyssens 后台扩展UI,可以在这里 ...

  2. SSH Session Recorder

    If you want to record your root ssh session  create a file .bash_profile  . and copy below line by l ...

  3. getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用

    getCacheDir()方法用于获取/data/data/<application package>/cache目录 getFilesDir()方法用于获取/data/data/< ...

  4. ceph优秀博文

    ceph官方博文: http://ceph.com/community/blog/ rgw根据rgw用户来分pool存放数据 http://cephnotes.ksperis.com/blog/201 ...

  5. 微软URLRewriter.dll的url重写在.net简单使用

    最近在做一个cms的网站 打算做成伪静态,从博客园上差了很多人的资料,终于实验成功了,原理就不讲了,附上在本地的配置,IIS的配置遇到后在发布. 文章最后附上源码 步骤如下 1.新建网站,添加URLR ...

  6. hive 使用脚本清洗数据:时间戳转日期

    import sys import datetime for line in sys.stdin: line = line.strip() userid, movieid, rating, unixt ...

  7. 我使用过的Linux命令

    我使用过的Linux命令之tee - 重定向输出到多个文件 用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们就不能看到输出了,如果我们既想把输出 ...

  8. JavaScript 入門

    <html lang="en"> <head>   <meta charset="UTF-8">   <meta na ...

  9. CSS 与 HTML5 响应式图片

    什么是响应式图片? 响应式图片是指:用户代理根据输出设备的分辨率不同加载不同类型的图片,不会造成带宽的浪费.同时,在改变输出设备类型或分辨率时,能及时加载对应类型的图片. CSS3 响应式图片 对于很 ...

  10. ASP中 Request.Form中文乱码的解决方法

    分享下解决方法直接用request.Form()获取的是所有数据所以会有乱码(具体原因不祥) 用 VBScript code Foreach obj in Request.Form Response. ...