国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Codevs 1242 布局 2005年USACO(差分約束)

2019-11-11 02:37:47
字體:
供稿:網(wǎng)友

1242 布局 2005年USACO 時(shí)間限制: 1 s 空間限制: 128000 KB 題目等級(jí) : 黃金 Gold 題目描述 Description 當(dāng)排隊(duì)等候喂食時(shí),奶牛喜歡和它們的朋友站得靠近些。FJ有N(2<=N<=1000)頭奶牛,編號(hào)從1到N,沿一條直線站著等候喂食。奶牛排在隊(duì)伍中的順序和它們的編號(hào)是相同的。因?yàn)槟膛O喈?dāng)苗條,所以可能有兩頭或者更多奶牛站在同一位置上。即使說,如果我們想象奶牛是站在一條數(shù)軸上的話,允許有兩頭或更多奶牛擁有相同的橫坐標(biāo)。 一些奶牛相互間存有好感,它們希望兩者之間的距離不超過一個(gè)給定的數(shù)L。另一方面,一些奶牛相互間非常反感,它們希望兩者間的距離不小于一個(gè)給定的數(shù)D。給出ML條關(guān)于兩頭奶牛間有好感的描述,再給出MD條關(guān)于兩頭奶牛間存有反感的描述。(1<=ML,MD<=10000,1<=L,D<=1000000) 你的工作是:如果不存在滿足要求的方案,輸出-1;如果1號(hào)奶牛和N號(hào) 奶牛間的距離可以任意大,輸出-2;否則,計(jì)算出在滿足所有要求的情況下,1號(hào)奶牛和N號(hào)奶牛間可能的最大距離。 輸入描述 Input Description Line 1: Three space-separated integers: N, ML, and MD. Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart. 輸出描述 Output Description Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N. 樣例輸入 Sample Input 4 2 1 1 3 10 2 4 20 2 3 3 樣例輸出 Sample Output 27 數(shù)據(jù)范圍及提示 Data Size & Hint 分類標(biāo)簽 Tags 最短路 圖論 USACO 2005年

/*差分約束.由約束條件可得(1)dis[y1]-dis[x1]<=T.(2)dis[x2]-dis[y2]<=-D.(3)dis[i]-dis[i+1]>=0.建圖后spfa跑最短路.*/#include<iostream>#include<cstdio>#include<queue>#include<cstring>#define MAXN 500001using namespace std;struct data{int v,next,x;}e[MAXN*3];int n,m,dis[MAXN],head[MAXN],cut,k,c[MAXN];bool b[MAXN];int read(){ int x=0;char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x;}void add(int u,int v,int z){ e[++cut].v=v; e[cut].x=z; e[cut].next=head[u]; head[u]=cut;}int spfa(){ queue<int>q;q.push(1); memset(dis,127/3,sizeof(dis)); dis[1]=0; while(!q.empty()) { int u=q.front();q.pop();b[u]=false; for(int i=head[u];i;i=e[i].next) { int v=e[i].v; if(dis[v]>dis[u]+e[i].x) { dis[v]=dis[u]+e[i].x; if(!b[v]) { b[v]=true,q.push(v);c[v]++; if(c[v]>=n) return -1;; } } } } if(dis[n]==dis[0]) return -2; return dis[n];}int main(){ int x,y,z; n=read(),m=read(),k=read(); for(int i=1;i<=m;i++) { x=read(),y=read(),z=read(); add(x,y,z); } for(int i=1;i<=k;i++) { x=read(),y=read(),z=read(); add(y,x,-z); } for(int i=1;i<=n;i++) add(i+1,i,0);
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉义市| 樟树市| 万宁市| 汝阳县| 龙门县| 青神县| 射阳县| 增城市| 平舆县| 曲麻莱县| 沙河市| 同江市| 承德县| 久治县| 民权县| 宜兴市| 息烽县| 赤城县| 永春县| 纳雍县| 永登县| 北辰区| 仪征市| 大丰市| 龙南县| 南丹县| 泽普县| 安吉县| 昔阳县| 云阳县| 全州县| 呼玛县| 福海县| 梁平县| 喀喇沁旗| 马关县| 中江县| 天全县| 共和县| 长沙市| 上思县|