//差分约束  >=求最长路径 <=求最短路径   结果都一样
//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. 转: ES6异步编程:Generator 函数的含义与用法

    转: ES6异步编程:Generator 函数的含义与用法 异步编程对 JavaScript 语言太重要.JavaScript 只有一根线程,如果没有异步编程,根本没法用,非卡死不可. 以前,异步编程 ...

  2. 面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ

    一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignme ...

  3. kafka学习(三)-kafka集群搭建

    kafka集群搭建 下面简单的介绍一下kafka的集群搭建,单个kafka的安装更简单,下面以集群搭建为例子. 我们设置并部署有三个节点的 kafka 集合体,必须在每个节点上遵循下面的步骤来启动 k ...

  4. 适配iPad的操作表sheet

    在 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"上传文件" message:@ ...

  5. 同一TextView上内容的不同显示

    首先请原谅我不会给文章起名字. . .不能准确的表达出究竟要讲什么,真实智商捉急. 直接上图 如图所看到的显示的是两个textview 第一个实现的是,在同一个textview中给不同内容赋予不同的颜 ...

  6. .NET读取Project 2007 MPP项目文件

    Project文件读取: 方法1:Microsoft.Project.OLEDB.11.0 string strConn = "Provider=Microsoft.Project.OLED ...

  7. CheckBox只选择一项

    最近做一个问卷的页面,客户那边说要使用checkbox而且只能选择一项 就写了下面的代码 <html xmlns="http://www.w3.org/1999/xhtml" ...

  8. 查询离指定日期最近的一条数据(oracle)

    select * from ( Select   *   from   t_currency_rate   where f_orig_curr='USD'   and f_dest_curr='RMB ...

  9. 如何为你的美术妹子做Unity的小工具(三)

    绘制脚本组件监视面板的内容 我们写了一个脚本Test using UnityEngine; using System.Collections; using System.Collections.Gen ...

  10. 基础知识——Cocos2d-x学习历程(三)

    1.场景与流程控制 我们把一些内容相对不变的游戏元素集合称作场景(scene),把游戏在场景之间切换的过程叫做流程控制(flow control). 在Cocos2d-x中,场景的实现是Scene. ...