POJ 3263 Tallest Cow

Tallest Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1815 Accepted: 822

Description

FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of that cow.

FJ has made a list of (0 ≤ R ≤ 10,000) lines of the form "cow 17 sees cow 34". This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.

For each cow from 1..N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

Input

Line 1: Four space-separated integers:  NIH and  R 
Lines 2.. R+1: Two distinct space-separated integers  A and  B (1 ≤  AB ≤  N), indicating that cow  A can see cow  B.

Output

Lines 1.. N: Line  i contains the maximum possible height of cow  i.

Sample Input

9 3 5 5
1 3
5 3
4 3
3 7
9 8

Sample Output

5
4
5
3
4
4
5
5
5

Source

USACO 2007 January Silver



原题的意思大概就是
一队牛1..N  每一个牛i都有一个高度Hi
告诉你一个关系x   y
表示y可以看见x
y可以看见x的条件是
Hy>=Hx
Hy>Hi    x<i<y
Hx>Hi    x<i<y
给你一头牛的高度(并且它是最高的)
求每头牛的最高高度


一开始想到的就是差分约束
连边
(2表示>=  1表示>  -1表示<  -2表示<=
从已知牛连所有牛一条边权2的边
边权不是实际差 而是表示关系)
上面想法貌似有问题
今天重新研究了下差分约束
差分约束有两种形式
一种求最大值 对应<=  求最短路
一种求最小值 对应>=  求最长路

对于第一种形式  
x-y<=k
可以转化成 x<=y+k  连接一条y到x权为k的边 然后跑最短路
第二种形式
x-y>=k
可以转化成 x>=y+k  连接一条y到x权为k的边 然后跑最长路
使用SPFA跑最短/最长路

可以过8个点 1个TLE 约1.5s  一个MLE
同样需要判重
不判重居然可以过9个 伤心了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
const int len=10111;
const int lim=1000001;
const int inf=999999999;

int m,n,num,tall,see;
int a,b,c,x,y;

struct self{int x,y;short int w;}s[lim];  
int first[lim],next[lim];

int d[len];
queue<int>q;
bool inq[len];

void makeside(int x,int y,int w)
{
        n++;
        s[n].x=x;s[n].y=y;s[n].w=w;
        next[n]=first[x];first[x]=n;
}

void spfa()
{
    q.push(0);
    for(a=1;a<=m;a++)d[a]=inf;
    inq[0]=1;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        inq[u]=0;
        for(int e=first[u];e!=-1;e=next[e])
        if(d[s[e].y]>d[u]+s[e].w)
        {
            d[s[e].y]=d[u]+s[e].w;
            if(!inq[s[e].y])
            {
                q.push(s[e].y);
                inq[s[e].y]=1;
            }
        }
    }
}
            
void print()
{
    int a,t=tall-d[num];
    for(a=1;a<=m;a++)cout<<d[a]+t<<'\n';
}

int main()
{
    freopen("tallest.in","r",stdin);
    freopen("tallest.out","w",stdout);
    memset(first,-1,sizeof(first));
    memset(next,-1,sizeof(next));
    
    scanf("%d%d%d%d",&m,&num,&tall,&see);
    for(a=1;a<=m;a++)makeside(0,a,0);
    for(a=1;a<=see;a++)
    {
        scanf("%d%d",&x,&y); 
        makeside(y,x,0);
        if(x>y)swap(x,y);
        for(b=x+1;b<y;b++)
        {
            makeside(x,b,-1);
            makeside(y,b,-1);
        }
    }
    
    spfa();
    print();
    return 0;
}




AC的程序
对于x y之间的牛全部高度-1
每一组x y相互之间不交叉 原因如下
如果存在x1<x2<y1<y2
Hx2<Hy1   x1<x2<y1
Hy1<Hx2   x2<y1<y2
出现矛盾

另外x看见y和y看见x效果是一样的 都是x y之间的牛高度-1
所以对于给的x y要判重
这里用到了set判重

j奶牛的高度tall已知
Hi与Hj是一个相对高度
Hi的实际高度为(Hi-Hj)+tall

对于x y之间的-1
有3种方法
1. i=x+1;i<y;i++
2. 线段树
3. 差分处理 类似于NOIP2012的借教室
    若b[i]=a[i]-a[i-1] 则Σb[i](1<=i<=n)=a[i]
    a[i]-1, x<i<y可以转化成 b[x+1]-1;b[y]+1;
    
因为比较懒 只写了个循环-1的程序。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
set<int>se;
int m,num,tall,see;
int a,b,c;
int x,y;
int s[11111];

bool ok(int l,int r)
{
    int add=l*10001+r;
    if(se.insert(add).second)return true;
    return false;
}

void dec(int l,int r)
{
    for(int a=l;a<=r;a++)s[a]--;
}

void print()
{
    int a;
    for(a=1;a<=m;a++)cout<<(s[a]-s[num])+tall<<'\n';
}

int main()
{
    freopen("tallest.in","r",stdin);
    freopen("tallest.out","w",stdout);
    scanf("%d%d%d%d",&m,&num,&tall,&see);
    for(a=1;a<=see;a++)
    {
        scanf("%d%d",&x,&y);
        if(x>y)swap(x,y);
        if(ok(x,y))
        dec(x+1,y-1);
    }
    
    print();
    return 0;
}




另外附上一份差分约束的程序 不是我写的 1个点WA 其他过的很快

var
   i,j,k,m,n,anx,max,x,y,z,h,t,s,hm,r,tmp:longint;
   a,b,c,f,d,aa:array[0..500000]of longint;
   q:array[-10..3000000]of longint;
   v:array[-10..100000]of boolean;

procedure qsort(l,r:longint);
var i,j,mid,p:longint;
begin
     i:=l;j:=r;mid:=a[(l+r)shr 1];
	 repeat
	      while(a[i]<mid)do inc(i);
		  while(a[j]>mid)do dec(j);
		  if(i<=j)then
		  begin
		      p:=a[i];a[i]:=a[j];a[j]:=p;
			  p:=b[i];b[i]:=b[j];b[j]:=p;
			  p:=c[i];c[i]:=c[j];c[j]:=p;
			  inc(i);
			  dec(j);
		  end;
	 until i>j;
	 if(i<r)then qsort(i,r);
	 if(l<j)then qsort(l,j);
end;

begin
    assign(input,'tallest.in');assign(output,'tallest.out');reset(input);rewrite(output);
	readln(n,k,hm,r);
	for i:=1 to r do
	begin
	    readln(x,y);
		inc(m);
		a[m]:=y;b[m]:=x;c[m]:=0;
		if x<y then
		for j:=x+1 to y-1 do
		begin
		    inc(m);a[m]:=x;b[m]:=j;c[m]:=-1;
		end;
		if(x>y)then
		for j:=y+1 to x-1 do
		begin
		    inc(m);a[m]:=x;b[m]:=j;c[m]:=-1;
		end;
	end;
	qsort(1,m);
	f[0]:=1;j:=1;
	for i:=1 to n do
	begin
	   while(a[j]=i)do inc(j);
	   f[i]:=j;
	end;
	
	h:=0;t:=0;
	fillchar(v,sizeof(v),true);
	fillchar(d,sizeof(d),0);
	for i:=1 to n do
        begin
            inc(t);
            q[t]:=i;
        end;
	while not(h=t)do
	begin
	    inc(h);
            x:=q[h];
            v[x]:=false;
            if(h>3000000)
            then h:=1;
		for i:=f[x-1] to f[x]-1 do
		begin
		    if d[x]+c[i]<d[b[i]]then
			begin
			    d[b[i]]:=c[i]+d[x];
				if not v[b[i]] then
				begin
				    v[b[i]]:=true;
					inc(t);
					if(t>3000000)then t:=1;
					q[t]:=b[i];
				end;
			end;
		end;
	end;
	for i:=1 to n do aa[i]:=hm+d[i];
	for i:=1 to n do writeln(aa[i]);
	close(input);
	close(output);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值