ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样
//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)的更多相关文章
- ZOJ 2770 Burn the Linked Camp 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- zoj Burn the Linked Camp (查分约束)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- ZOJ2770 Burn the Linked Camp(差分约束系统)
区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...
- zoj2770 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...
- ZOJ 2770 差分约束+SPFA
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
随机推荐
- Android下pm 命令详解
Sam在看相关PackageManager代码时,无意中发现Android 下提供一个pm命令,通常放在/system/bin/下.这个命令与Package有关,且非常实用.所以研究之.0. Usag ...
- C++多线程编程(三)线程间通信
多线程编程之三——线程间通讯 作者:韩耀旭 原文地址:http://www.vckbase.com/document/viewdoc/?id=1707 七.线程间通讯 一般而言,应用程序中的一个次要线 ...
- uvalive5818 uva12376 As Long as I Learn, I Live
题意:给出一个又向图每个图有权值和编号(正方形里的是编号),从第0号节点开始每次向当前节点所连的点中权值最大的节点移动(不会存在权值相同的节点),问最后所在的节点编号和经过的节点的权值之和. 解:模拟 ...
- centos ldap
Standalone LDAP Daemon, slapd(standalone lightweight access protocol)Lightweight Directory Access Pr ...
- 网易云课堂_C语言程序设计进阶_第四周:ACL图形库
创建ACLLib程序 #include"acllib.h" #include<stdio.h> int Setup1() { initWindow(, );//初始化窗 ...
- Matrix Swapping II(求矩阵最大面积,dp)
Matrix Swapping II Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Crisis of HDU(母函数)
Crisis of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 关于cocos2d安装时编译不成功(个人心得)
在解压cocos2d执行vs2010.sln时错误发生不能成功生成.遇到这样的错误: 1>c:\program files\microsoft sdks\windows\v7.0a\includ ...
- DataTable 修改列名 删除列 调整列顺序
DataTable myDt =dt;//删除列myDt.Columns.Remove("minArea");myDt.Columns.Remove("maxArea&q ...
- socketio 握手前中断报错
前两天折腾了下socketio,部署完发现通过nginx代理之后前端的socket无法和后端通信了,于是暴查一通,最后解决问题: location / { proxy_pass http://127. ...