POJ 1860(判定正圈)

Bellman_ford

Program P1860;
var
   n,m,i,j,s:longint;
   v:double;
   flag:boolean;
   d:array[1..100] of double;
   x,y:array[1..100] of longint;
   map:array[1..100,1..4] of double;
procedure relax(i:longint);
begin
   if (d[y[i]]<(d[x[i]]-map[i,2])*map[i,1]) then
   begin
      d[y[i]]:=(d[x[i]]-map[i,2])*map[i,1];
      flag:=false;
   end;
   if (d[x[i]]<(d[y[i]]-map[i,4])*map[i,3]) then
   begin
      d[x[i]]:=(d[y[i]]-map[i,4])*map[i,3];
      flag:=false;
   end;

end;
procedure bell_ford;
var
   i,j:longint;
begin
   for i:=1 to n do
   begin
      flag:=true;
      for j:=1 to m do relax(j);
      if flag then break;
   end;
   if flag then writeln('NO')
   else writeln('YES');
end;
begin
   while not seekeof do
   begin
      readln(n,m,s,v);
      fillchar(d,sizeof(d),0);
      d[s]:=v;
      for i:=1 to m do
      begin
         readln(x[i],y[i],map[i,1],map[i,2],map[i,3],map[i,4]);
      end;
      bell_ford;
   end;
end.

POJ 1207(3N+1)

3n+1问题 果断暴力

Program P1207;
var
   i,j,k,n,m,ans:longint;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;
procedure swap(var a,b:longint);
var
   p:longint;
begin
   p:=a;
   a:=b;
   b:=p;
end;
begin
   while not eof do
   begin
      readln(n,m);
      write(n,' ',m,' ');
      if n>m then swap(n,m);
      ans:=0;
      for i:=n to m do
      begin
         j:=1;
         k:=i;
         while (k<>1) do
         begin
            if (k mod 2=0) then k:=k div 2
            else k:=k*3+1;
            inc(j);
         end;

         ans:=max(ans,j);
      end;
      writeln(ans);
   end;
end.

POJ 1328(贪心)

贪心,区间排序

如果区间包含 略过,否则答案+1;

Program P1328;
var
   n,d,i,j,k,ans:longint;
   x:double;
   tag:boolean;
   map:array[1..1000,1..2] of double;
procedure qsort(l,r:longint);
var
   i,j:longint;
   m,p:double;
begin
   i:=l;
   j:=r;
   m:=map[(l+r) div 2,1];
   repeat
      while map[i,1]<m do inc(i);
      while map[j,1]>m do dec(j);
      if i<=j then
      begin
         p:=map[i,1];
         map[i,1]:=map[j,1];
         map[j,1]:=p;
         p:=map[i,2];
         map[i,2]:=map[j,2];
         map[j,2]:=p;
         inc(i);
         dec(j);
      end;
   until i>j;
   if l<j then qsort(l,j);
   if i<r then qsort(i,r);
end;
begin
   i:=1;
{   assign(input,'P1328.in');
   assign(output,'P1328.out');
   reset(input);
   rewrite(output);   }
   while not seekeof do
   begin
      tag:=true;
      read(n,d);
      if d<=0 then tag:=false;
      if (n=0) and (d=0) then break;
      j:=1;
      for k:=1 to n do
      begin
         read(map[j,1],map[j,2]);
         if map[j,2]<0  then continue;
         if d<map[j,2] then continue;
         x:=sqrt(d*d-map[j,2]*map[j,2]);
         map[j,2]:=map[j,1]+x;
         map[j,1]:=map[j,1]-x;
         inc(j);
      end;
      dec(j);
      if tag and (j=n) then
      begin
         qsort(1,j);
         ans:=1;
         x:=map[1,2];
         for k:=2 to j do
         begin
            if (map[k,2]<x) then
            begin
               x:=map[k,2];
            end
            else if (x<map[k,1]) then
            begin
               inc(ans);
               x:=map[k,2];
            end;

         end;

         writeln('Case ',i,': ',ans);
      end
      else writeln('Case ',i,': -1');
      inc(i);
   end;
 {  close(input);
   close(output);   }
end.

POJ 3006(线性筛素数)

线性筛素数……

Program P3006;
const
   maxn=1000000;
var
   prime:array[1..maxn] of boolean;
   p:array[1..maxn] of longint;
   t:longint;
   a,d,n:longint;
Procedure primeing;
var
   i,j:longint;
begin
   fillchar(prime,sizeof(prime),false);
   t:=0;
   prime[1]:=true;
   for i:=2 to maxn do
   begin
      if not(prime[i]) then
      begin
         inc(t);
         p[t]:=i;
      end;
      for j:=1 to t do
      begin
         if p[j]*i>maxn then break;
         prime[p[j]*i]:=true;
         if i mod p[j]=0 then break;
      end;
   end;
end;
begin
   primeing;
   readln(a,d,n);
   while (a+d+n>0) do
   begin
      while (n>0) do
      begin
         if not(prime[a]) then dec(n);
         inc(a,d);
      end;
      writeln(a-d);
      readln(a,d,n);
   end;
end.

POJ 2983(差分约束-有无解)

差分约束……

Program p2983;
var
   n,m,i,j,p:longint;
   c:char;
   flag:boolean;
   d:array[0..5000] of longint;
   w:array[1..100100,1..3] of longint;
procedure relax(v,u,w:longint);
begin
   if (d[v]>d[u]+w) then
   begin
      d[v]:=d[u]+w;
      flag:=false;
   end;
end;
procedure bellman_ford;
var
   i,j,k:longint;
begin
   flag:=true;
// w[i,3]=-1 w[i,2]-w[i,1]>0 ->w[i,1]-w[i,2]<0 ->w[i,1]-w[i,2]<=-1
// w[i,3]>-1 w[i,2]-w[i,1]=w[i,3] ->w[i,2]-w[i,1]<=w[i,3] w[i,1]-w[i,2]<=-w[i,3]
   for k:=1 to n+1 do
   begin
      flag:=true;
      for i:=1 to m do
      begin
         if w[i,3]=-1 then
         begin
            relax(w[i,1],w[i,2],-1);
         end
         else
         begin
            relax(w[i,2],w[i,1],w[i,3]);
            relax(w[i,1],w[i,2],-w[i,3]);
         end;

      end;
      if flag then break;
   end;

   if flag then writeln('Reliable')
   else writeln('Unreliable');
end;
begin
   while not seekeof do
   begin
      fillchar(d,sizeof(d),0);
      readln(n,m);
      for i:=1 to m do
      begin
         read(c);
         if (c='p') or (c='P') then
         begin
            readln(w[i,1],w[i,2],w[i,3]);
         end
         else
         begin
            readln(w[i,1],w[i,2]);
            w[i,3]:=-1;
         end;

      end;
      bellman_ford;

   end;
end.

POJ 1201 (差分约束)

1176 加权版 

……还是差分约束

Program P1201;
var
   n,i,j,minq,maxq:longint;
   d:array[-1..60000] of longint;
   w:array[1..60000,1..3] of longint;
   flag:boolean;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;

procedure relax(v,u,w:longint);
begin
   if (d[u]+w<d[v]) then
   begin
      d[v]:=d[u]+w;
      flag:=false;
   end;
end;
procedure bellman_ford;
var
   i,j,k:longint;
begin
   //d[w[i,2]] - d[w[i,1]-1]>=2 ->d[w[i,1]-1]-d[w[i,2]]<=-2
   //d[i]-d[i-1]<=1
   //d[i]-d[i-1]>=0 ->d[i-1]-d[i]<=0

   d[minq-1]:=0;
   while (true) do
   begin
      flag:=true;
//    for i:=minq to maxq do relax(i,minq-1,0);
      for i:=1 to n do relax(w[i,1]-1,w[i,2],-w[i,3]);
      for i:=minq to maxq do relax(i,i-1,1);
      for i:=maxq downto minq do relax(i-1,i,0);
      if flag then break;
   end;
end;
begin
   while not seekeof do
   begin
      minq:=maxlongint;  maxq:=0;
      fillchar(d,sizeof(d),0);
      read(n);
      for i:=1 to n do
      begin
         read(w[i,1],w[i,2],w[i,3]);
         minq:=min(minq,w[i,1]);
         maxq:=max(maxq,w[i,2]);
      end;
      bellman_ford;
      writeln(d[maxq]-d[minq-1]);
   end;
end.

POJ 1716 (差分约束)

差分约束……

Program P1716;
var
   n,i,j,minq,maxq:longint;
   d:array[-1..10000] of longint;
   w:array[1..30000,1..2] of longint;
   flag:boolean;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;

procedure relax(v,u,w:longint);
begin
   if (d[u]+w<d[v]) then
   begin
      d[v]:=d[u]+w;
      flag:=false;
   end;
end;
procedure bellman_ford;
var
   i,j,k:longint;
begin
   //d[w[i,2]] - d[w[i,1]-1]>=2 ->d[w[i,1]-1]-d[w[i,2]]<=-2
   //d[i]-d[i-1]<=1
   //d[i]-d[i-1]>=0 ->d[i-1]-d[i]<=0

   d[minq-1]:=0;
   while (true) do
   begin
      flag:=true;
//    for i:=minq to maxq do relax(i,minq-1,0);
      for i:=1 to n do relax(w[i,1]-1,w[i,2],-2);
      for i:=minq to maxq do relax(i,i-1,1);
      for i:=maxq downto minq do relax(i-1,i,0);
      if flag then break;
   end;
end;
begin
   while not seekeof do
   begin
      minq:=maxlongint;  maxq:=0;
      fillchar(d,sizeof(d),0);
      read(n);
      for i:=1 to n do
      begin
         read(w[i,1],w[i,2]);
         minq:=min(minq,w[i,1]);
         maxq:=max(maxq,w[i,2]);
      end;
      bellman_ford;
      writeln(d[maxq]-d[minq-1]);
   end;
end.

POJ 1149(构图巨难……)

EK……

Program P1149;
var
   i,j,k,n,m,p:longint;
   map,f:array[0..500,0..500] of longint;
   visit,e,pre,pighouse:array[0..500] of longint;
   maxflow:longint;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;

procedure edmons_karp;
var
   i,j:longint;
   h,t:longint;
   queue:array[1..500] of longint;
begin
   maxflow:=0;

   while (true) do
   begin
      fillchar(pre,sizeof(pre),0);
      fillchar(e,sizeof(e),0);
      queue[1]:=0;
      e[0]:=maxlongint;
      h:=1;
      t:=1;
      while h<=t do
      begin
         for i:=1 to n+1 do
            if (map[queue[h],i]-f[queue[h],i]>0) and (e[i]=0) then
            begin
               e[i]:=min(e[queue[h]],map[queue[h],i]-f[queue[h],i]);
               pre[i]:=queue[h];
               inc(t);
               queue[t]:=i;
            end;
         if e[n+1]>0 then break;
         inc(h);
      end;

      if (e[n+1]=0) then break;

      i:=n+1;
      while (true) do
      begin
         inc(f[pre[i],i],e[n+1]);
         f[i,pre[i]]:=-f[pre[i],i];
         if i=0 then break;
         i:=pre[i];
      end;
      inc(maxflow,e[n+1]);
   end;

end;
begin
   read(m,n);
   fillchar(visit,sizeof(visit),0);
   fillchar(f,sizeof(f),0);
   fillchar(map,sizeof(map),0);
   fillchar(e,sizeof(e),0);


   for i:=1 to m do read(pighouse[i]);
   for i:=1 to n do
   begin
      read(k);
      for j:=1 to k do
      begin
         read(p);
         if (visit[p]=0) then
         begin
            map[0,i]:=map[0,i]+pighouse[p];
            visit[p]:=i;
         end
         else
         begin
            map[visit[p],i]:=maxlongint;
         end;
      end;
      read(p);
      map[i,n+1]:=p;

   end;

   edmons_karp;
   writeln(maxflow);

end.

POJ 1273(网络流-附hllp+sap模板)

网络流入门题目

Program P1273;
Var
   n,m,i,j,x,y,p,level:longint;
   map,f:array[1..400,1..400] of longint;
   list:array[0..400,0..400] of longint;
   queue:array[1..400] of longint;
   b:array[1..400] of boolean;
   d,e:array[1..400] of longint;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;
procedure dijstra;
var
   h,t,i:longint;
begin
   queue[1]:=n;
   b[n]:=true;
   h:=1;
   t:=1;
   while h<=t do
   begin
      for i:=1 to n-1 do
         if (map[i,queue[h]]>0) and (not(b[i])) then
         begin
            inc(t);
            queue[t]:=i;
            b[i]:=true;
            d[i]:=d[queue[h]]+1;
         end;
      inc(h);
   end;
   d[1]:=n+1;
end;
procedure push(i,j:longint);
var
   flow:longint;
begin
   flow:=min(map[i,j]-f[i,j],e[i]);

   if (e[j]=0) and (j<>1) and (j<>n) then
   begin
      inc(list[d[j],0]);
      list[d[j],list[d[j],0]]:=j;
      level:=max(level,d[j]);
   end;

   dec(e[i],flow);
   inc(e[j],flow);
   inc(f[i,j],flow);
   f[j,i]:=-f[i,j];
end;
procedure relable(i:longint);
var
   j,minj:longint;
begin
   minj:=maxlongint;
   for j:=1 to n do
      if (map[i,j]-f[i,j]>0) and (b[j]) then
         minj:=min(minj,d[j]);
   d[i]:=minj+1;

   level:=d[i];
   list[level,0]:=1;
   list[level,1]:=i;
end;
procedure sa;
var
   i,j,minj:longint;
   tag:boolean;
begin
   dijstra;
   level:=0;
   for i:=1 to n do
      if (map[1,i]>0) and (b[i]) then
      begin
         dec(e[1],map[1,i]);
         inc(e[i],map[1,i]);
         inc(f[1,i],map[1,i]);
         f[i,1]:=-f[1,i];
         inc(list[d[i],0]);
         list[d[i],list[d[i],0]]:=i;
         level:=max(level,d[i]);
      end;
   while (level>0) do
   begin
      i:=list[level,list[level,0]];
      dec(list[level,0]);
      while (level>0) and (list[level,0]=0) do dec(level);

      for j:=1 to n do
         if (map[i,j]-f[i,j]>0) and (d[i]=d[j]+1) and (b[j]) then
         begin
            push(i,j);
            if e[i]=0 then break;
         end;
      if e[i]>0 then relable(i);
   end;
end;
begin
   while not eof do
   begin
      fillchar(map,sizeof(map),0);
      fillchar(f,sizeof(f),0);
      fillchar(d,sizeof(d),0);
      fillchar(b,sizeof(b),false);
      fillchar(list,sizeof(list),0);
      fillchar(e,sizeof(e),0);
      readln(m,n);
      for i:=1 to m do
      begin
         readln(x,y,p);
         inc(map[x,y],p);
      end;
      sa;
      writeln(e[n]);
   end;
end.

Sap:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cstdlib>
#include<cstring>
using namespace std;
#define MAXN (200000+10)
#define MAXNnode (200000+10)
#define MAXedge (200000+10)
#define INF (2139062143)
int n,m,size,edge[MAXedge],pre[MAXNnode],next[MAXedge],weight[MAXedge],s,t;
int un(int p){return (p%2)?(p+1):p-1;}
void addedge(int u,int v,int w)
{
     edge[++size]=v;weight[size]=w;
     next[size]=pre[u];pre[u]=size;
     edge[++size]=u;weight[size]=0;
     next[size]=pre[v];pre[v]=size;
}
int q[MAXN*10],d[MAXN*2],pre2[MAXN],cnt[MAXN];
void spfa()
{
    int head=1,tail=1;
    memset(d,127,sizeof(d));
    q[1]=t;d[t]=0;
    while (head<=tail)
    {
        int &u=q[head];
        for (int p=pre[u];p;p=next[p])
        {
            int &v=edge[p];
            if (d[v]>d[u]+1)
            {
               d[v]=d[u]+1;
               q[++tail]=v;
               cnt[d[v]]++;
            }
        }
        head++;
    }

}
int relable(int u)
{
    int minj=n;
    for (int p=pre[u];p;p=next[p])
        if (weight[p]&&d[edge[p]]!=INF) minj=min(minj,d[edge[p]]+1);
    return minj;
}
long long sap()
{
     spfa();
     int now=s;
	 long long nowflow=INF,maxflow=0;
     while (1)
     {
         int flag=0;
         for (int p=pre[now];p;p=next[p])
         {
             int &v=edge[p];
             if (weight[p]&&d[v]==d[now]-1)
             {
                 flag=v;
                 pre2[v]=p;
                 now=v;
                 break;
             }
         }
         if (flag)
         {
            if (now==t)
            {
               nowflow=INF;
               for (int p=now;p!=s;p=edge[un(pre2[p])])
                   nowflow=min(nowflow,(long long )weight[pre2[p]]);
               maxflow+=nowflow;
               for (int p=now;p!=s;p=edge[un(pre2[p])])
               {
                   weight[pre2[p]]-=nowflow;
                   weight[un(pre2[p])]+=nowflow;
               }
               now=s;
               memset(pre2,0,sizeof(pre2));
            }
         }
         else
         {
             if (--cnt[d[now]]==0) break;
             d[now]=relable(now);
             cnt[d[now]]++;
             if (now!=s) now=edge[un(pre2[now])];
     	}
     }
     return maxflow;
}
int main()
{
	while (scanf("%d%d",&m,&n)==2)
	{
	    memset(pre,0,sizeof(pre));
	    memset(next,0,sizeof(next));
	    memset(edge,0,sizeof(edge));
	    memset(weight,0,sizeof(weight));
    	    memset(cnt,0,sizeof(cnt));
	    size=0; s=1;t=n;
            for (int i=1;i<=m;i++)
	    {
    	        int u,v,w;
        	cin>>u>>v>>w;
  	        addedge(u,v,w);
    	    }
        if (m==0) cout<<'0'<<endl;
	    else cout<<sap()<<endl;
	}
	return 0;
}

POJ 1325(2部图匹配)

2部图匹配,不能考虑0

Program P1325;
var
   n,m,k,i,j:longint;
   p,x,y,level:longint;
   map,f,list:array[-1..300,-1..300] of longint;
   d,e:array[-1..300] of longint;
   queue:array[1..300] of longint;
   b:array[-1..300] of boolean;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;

procedure dijstra;
var
   h,t,i:longint;
begin
   fillchar(d,sizeof(d),0);
   fillchar(queue,sizeof(queue),0);
   fillchar(b,sizeof(b),false);
   queue[1]:=n+m;
   b[n+m]:=true;
   h:=1;
   t:=1;
   while h<=t do
   begin
      for i:=-1 to n+m-1 do
      begin
         if (map[i,queue[h]]>0) then
            if (not(b[i])) then
         begin
            inc(t);
            queue[t]:=i;
            b[i]:=true;
            d[i]:=d[queue[h]]+1;
         end;
      end;
      inc(h);
   end;
   d[-1]:=n+m+2+1;
end;
procedure push(i,j:longint);
var
   flow:longint;
begin
   flow:=min(e[i],map[i,j]-f[i,j]);

   if (e[j]=0) and (j>-1) and (j<n+m) then
   begin
      inc(list[d[j],0]);
      list[d[j],list[d[j],0]]:=j;
      level:=max(level,d[j]);
   end;

   dec(e[i],flow);
   inc(e[j],flow);
   inc(f[i,j],flow);
   f[j,i]:=-f[i,j];

end;
procedure relable(i:longint);
var
   j,minj:longint;
begin
   minj:=maxlongint;
   for j:=-1 to n+m do
      if (map[i,j]-f[i,j]>0) and (b[j]) then minj:=min(minj,d[j]);
   d[i]:=minj+1;

   level:=d[i];
   inc(list[level,0]);
   list[level,1]:=i;
end;
Procedure hllp;
var
   i,j,flow:longint;
begin
   dijstra;

   level:=0;
   for i:=0 to n-1 do
      if b[i] then
      begin
         e[i]:=1;
         f[-1,i]:=1;
         f[i,-1]:=-1;


         inc(list[d[i],0]);
         list[d[i],list[d[i],0]]:=i;
         level:=max(level,d[i]);
      end;
   while (level>0) do
   begin
      i:=list[level,list[level,0]];
      dec(list[level,0]);
      while (level>0) and (list[level,0]=0) do dec(level);

      for j:=-1 to n+m do
         if (d[i]=d[j]+1) and (map[i,j]-f[i,j]>0) and (b[j]) then
         begin
            push(i,j);

            if e[i]=0 then break;
         end;
      if e[i]>0 then relable(i);

   end;

end;
begin
   read(n);
   while (n>0) do
   begin
      fillchar(list,sizeof(list),0);
      fillchar(e,sizeof(e),0);
      fillchar(f,sizeof(f),0);

      read(m,k);
      fillchar(map,sizeof(map),0);
      for i:=1 to k do
      begin
         read(p,x,y);
         if (x=0) or (y=0) then continue;
         map[x,n+y]:=1;
      end;
      for i:=0 to n-1 do map[-1,i]:=1;
      for i:=n to n+m-1 do map[i,n+m]:=1;
      hllp;
      writeln(e[n+m]);

      read(n);

   end;
end.