bzoj1001: [BeiJing2006]狼抓兔子

很明显是最小割。然而我不会算时间复杂度。据说会RE。然后得知了平面图转对偶图。spfa跑的好慢===遂又写了dijkstra===然而还是跑的好慢啊 啊啊

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define op() clr(head,0);pt=edges;
#define qwq(x) for(edge *o=head[x];o;o=o->next)
#define adde(u,v,d) add(u,v,d),add(v,u,d)
int read(){
	int x=0;char c=getchar();bool f=true;
	while(!isdigit(c)){
		if(c=='-') f=false;c=getchar();
	}
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return f?x:-x;
}
const int nmax=2000005;
const int maxn=6000005;
const int inf=0x7f7f7f7f;
struct edge{
	int to,dist;edge *next;
};
edge edges[maxn],*pt,*head[nmax];
int d[nmax],n,m;bool inq[nmax];
void add(int u,int v,int d){
	pt->to=v;pt->dist=d;pt->next=head[u];head[u]=pt++;
}
bool  init(){
	op();
	n=read(),m=read();int tmp,s=0,t=(n-1)*(m-1)*2+1;
	if(n==1||m==1){
		int ans=inf;
		if(n==1) rep(i,m-1) tmp=read(),ans=min(ans,tmp);
		if(m==1) rep(i,n-1) tmp=read(),ans=min(ans,tmp);
		printf("%d\n",ans);return false;
	}
	rep(i,m-1) tmp=read(),adde(i+i,t,tmp);
	REP(i,2,n-1) rep(j,m-1) tmp=read(),adde((i-1)*(m-1)*2+2*j,(i-2)*(m-1)*2+j+j-1,tmp);
	rep(i,m-1) tmp=read(),adde(s,(n-2)*(m-1)*2+i+i-1,tmp);
	rep(i,n-1){
		tmp=read();adde(s,(i-1)*(m-1)*2+1,tmp);
		REP(j,2,m-1) tmp=read(),adde((i-1)*(m-1)*2+j+j-2,(i-1)*(m-1)*2+j+j-1,tmp);
		tmp=read();adde(i*(m-1)*2,t,tmp);
	}
	rep(i,n-1) rep(j,m-1) tmp=read(),adde((i-1)*(m-1)*2+j+j-1,(i-1)*(m-1)*2+j+j,tmp);
	return true;
}
/*int spfa(int s,int t){
	queue<int>q;q.push(s);
	clr(inq,0);clr(d,0x7f);inq[s]=1;d[s]=0;
	while(!q.empty()){
		int x=q.front();q.pop();inq[x]=0;
		qwq(x) if(d[o->to]>d[x]+o->dist){
			d[o->to]=d[x]+o->dist;
			if(!inq[o->to]) q.push(o->to),inq[o->to]=1;
		}
	}
	return d[t];
}*/
struct node{
	int x,d;
	node(int x,int d):x(x),d(d){}
	bool operator<(const node&o) const{
	  return d>o.d;}
};
priority_queue<node>q;
int dijkstra(int s,int t){
	clr(d,0x7f);d[s]=0;
	q.push(node(s,0));
	while(!q.empty()){
		node o=q.top();q.pop();
		int x=o.x,dist=o.d;
		if(d[x]!=dist) continue;
		qwq(x) if(d[o->to]>d[x]+o->dist) 
		  d[o->to]=d[x]+o->dist,q.push(node(o->to,d[o->to]));
	}
	return d[t];
}
int main(){
	if(init()) printf("%d\n",dijkstra(0,(n-1)*(m-1)*2+1));
	return 0;
}

  

1001: [BeiJing2006]狼抓兔子

Time Limit: 15 Sec  Memory Limit: 162 MB
Submit: 17776  Solved: 4353
[Submit][Status][Discuss]

Description

现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,
而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形:

 

左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 
1:(x,y)<==>(x+1,y) 
2:(x,y)<==>(x,y+1) 
3:(x,y)<==>(x+1,y+1) 
道路上的权值表示这条路上最多能够通过的兔子数,道路是无向的. 左上角和右下角为兔子的两个窝,
开始时所有的兔子都聚集在左上角(1,1)的窝里,现在它们要跑到右下解(N,M)的窝中去,狼王开始伏击
这些兔子.当然为了保险起见,如果一条道路上最多通过的兔子数为K,狼王需要安排同样数量的K只狼,
才能完全封锁这条道路,你需要帮助狼王安排一个伏击方案,使得在将兔子一网打尽的前提下,参与的
狼的数量要最小。因为狼还要去找喜羊羊麻烦.

Input

第一行为N,M.表示网格的大小,N,M均小于等于1000.
接下来分三部分
第一部分共N行,每行M-1个数,表示横向道路的权值. 
第二部分共N-1行,每行M个数,表示纵向道路的权值. 
第三部分共N-1行,每行M-1个数,表示斜向道路的权值. 
输入文件保证不超过10M

Output

输出一个整数,表示参与伏击的狼的最小数量.

Sample Input

3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6

Sample Output

14

HINT

 

 2015.4.16新加数据一组,可能会卡掉从前可以过的程序。

 

Source

 
[Submit][Status][Discuss]
posted @ 2016-07-14 20:55  BBChq  阅读(298)  评论(0编辑  收藏  举报