HDOJ 3622 - Bomb Game 2-sat+二分....细心...
题意:
有N个炸弹..每个炸弹有两个位置可以选择..把炸弹放到其中一个地方去...炸弹的爆炸范围是其为圆心的圆...两个炸弹不能有攻击范围上的重合..问要满足条件..炸弹爆炸范围的半径最长能是多少...
题解:
每个炸弹看成一类..其在两个中比选一个..符合2-sat的构图条件....那么就二分枚举炸弹的爆炸范围..枚举相互是否干扰来做边构造2-sat模型...tarjan来判断是否合法..
题目不难..但是我2B了...在初始化vector时..for (i=0;i<(n<<1);i++) T[i].clear() 写成了for (i=0;i<(1<<n);i++) T[i].clear()...搞得我查了好久的错误..还写了几个版本的代码..这道题不好自己出数据..搞得很郁闷..最后怀疑是vector使用的问题..就索性写了个链表..结果就A了~~再反过头来看..发现了错误的根源...弱啊......
Program-----链表存图..整个程序int型计算..结果再开方除2输出
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> #define ll long long #define oo 1000000007 #define pi acos(-1.0) #define MAXN 205 #define MAXM 100005 using namespace std; struct node { int x0,y0,x1,y1; }b[MAXN]; struct LINE { int x,y,next; }line[MAXM]; int Lnum,_next[MAXN],dfn[MAXN],low[MAXN],tp[MAXN],tpnum,DfsIndex,arc[MAXN][MAXN]; bool instack[MAXN]; stack<int> mystack; int dis(int x0,int y0,int x1,int y1) { return (x0-x1)*(x0-x1)+(y0-y1)*(y0-y1); } void addline(int x,int y) { line[++Lnum].next=_next[x],_next[x]=Lnum; line[Lnum].x=x,line[Lnum].y=y; } void tarjan(int x) { int y,k; dfn[x]=low[x]=++DfsIndex; instack[x]=true; mystack.push(x); for (k=_next[x];k;k=line[k].next) { y=line[k].y; if (!dfn[y]) { tarjan(y); low[x]=min(low[x],low[y]); }else if (instack[y]) low[x]=min(low[x],dfn[y]); } if (dfn[x]==low[x]) { tpnum++; do { x=mystack.top(); mystack.pop(); instack[x]=false; tp[x]=tpnum; }while (dfn[x]!=low[x]); } return; } bool _2sat(int n,int d) { int i,j; Lnum=0; memset(_next,0,sizeof(_next)); for (i=0;i<n;i++) for (j=i+1;j<n;j++) { if (arc[i<<1][j<<1]<d) addline(i<<1,j<<1|1),addline(j<<1,i<<1|1); if (arc[i<<1][j<<1|1]<d) addline(i<<1,j<<1),addline(j<<1|1,i<<1|1); if (arc[i<<1|1][j<<1]<d) addline(i<<1|1,j<<1|1),addline(j<<1,i<<1); if (arc[i<<1|1][j<<1|1]<d) addline(i<<1|1,j<<1),addline(j<<1|1,i<<1); } memset(dfn,0,sizeof(dfn)); memset(instack,false,sizeof(instack)); while (!mystack.empty()) mystack.pop(); tpnum=DfsIndex=0; for (i=0;i<(n<<1);i++) if (!dfn[i]) tarjan(i); for (i=0;i<n;i++) if (tp[i<<1]==tp[i<<1|1]) return false; return true; } int main() { int n,i; while (~scanf("%d",&n)) { for (i=0;i<n;i++) scanf("%d%d%d%d",&b[i].x0,&b[i].y0,&b[i].x1,&b[i].y1); for (i=0;i<n;i++) for (int j=i+1;j<n;j++) { arc[i<<1][j<<1]=arc[j<<1][i<<1]=dis(b[i].x0,b[i].y0,b[j].x0,b[j].y0); arc[i<<1][j<<1|1]=arc[j<<1|1][i<<1]=dis(b[i].x0,b[i].y0,b[j].x1,b[j].y1); arc[i<<1|1][j<<1]=arc[j<<1][i<<1|1]=dis(b[i].x1,b[i].y1,b[j].x0,b[j].y0); arc[i<<1|1][j<<1|1]=arc[j<<1|1][i<<1|1]=dis(b[i].x1,b[i].y1,b[j].x1,b[j].y1); } int l,r,mid; l=0,r=2*oo; while (r-l>1) { mid=(r+l)/2; if (_2sat(n,mid)) l=mid; else r=mid; } printf("%.2lf\n",sqrt(l)*0.5); } return 0; }
Program-----vector存图..其他同上
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> #define ll long long #define oo 1000000007 #define pi acos(-1.0) #define MAXN 205 using namespace std; struct node { int x0,y0,x1,y1; }b[MAXN]; vector<int> T[MAXN]; int dfn[MAXN],low[MAXN],tp[MAXN],tpnum,DfsIndex,arc[MAXN][MAXN]; bool instack[MAXN]; stack<int> mystack; int dis(int x0,int y0,int x1,int y1) { return (x0-x1)*(x0-x1)+(y0-y1)*(y0-y1); } void tarjan(int x) { int i,y,m=T[x].size(); dfn[x]=low[x]=++DfsIndex; instack[x]=true; mystack.push(x); for (i=0;i<m;i++) { y=T[x][i]; if (!dfn[y]) { tarjan(y); low[x]=min(low[x],low[y]); }else if (instack[y]) low[x]=min(low[x],dfn[y]); } if (dfn[x]==low[x]) { tpnum++; do { x=mystack.top(); mystack.pop(); instack[x]=false; tp[x]=tpnum; }while (dfn[x]!=low[x]); } return; } bool _2sat(int n,int d) { int i,j; for (i=0;i<(n<<1);i++) T[i].clear(); //小错误..搞了N久才发现.. for (i=0;i<n;i++) for (j=i+1;j<n;j++) { if (arc[i<<1][j<<1]<d) T[i<<1].push_back(j<<1|1),T[j<<1].push_back(i<<1|1); if (arc[i<<1][j<<1|1]<d) T[i<<1].push_back(j<<1),T[(j<<1)|1].push_back(i<<1|1); if (arc[i<<1|1][j<<1]<d) T[(i<<1)|1].push_back((j<<1)|1),T[j<<1].push_back(i<<1); if (arc[i<<1|1][j<<1|1]<d) T[(i<<1)|1].push_back(j<<1),T[(j<<1)|1].push_back(i<<1); } memset(dfn,0,sizeof(dfn)); memset(instack,false,sizeof(instack)); while (!mystack.empty()) mystack.pop(); tpnum=DfsIndex=0; for (i=0;i<(n<<1);i++) if (!dfn[i]) tarjan(i); for (i=0;i<n;i++) if (tp[i<<1]==tp[i<<1|1]) return false; return true; } int main() { int n,i; while (~scanf("%d",&n)) { for (i=0;i<n;i++) scanf("%d%d%d%d",&b[i].x0,&b[i].y0,&b[i].x1,&b[i].y1); for (i=0;i<n;i++) for (int j=i+1;j<n;j++) { arc[i<<1][j<<1]=arc[j<<1][i<<1]=dis(b[i].x0,b[i].y0,b[j].x0,b[j].y0); arc[i<<1][j<<1|1]=arc[j<<1|1][i<<1]=dis(b[i].x0,b[i].y0,b[j].x1,b[j].y1); arc[i<<1|1][j<<1]=arc[j<<1][i<<1|1]=dis(b[i].x1,b[i].y1,b[j].x0,b[j].y0); arc[i<<1|1][j<<1|1]=arc[j<<1|1][i<<1|1]=dis(b[i].x1,b[i].y1,b[j].x1,b[j].y1); } int l,r,mid; l=0,r=2*oo; while (r-l>1) { mid=(r+l)>>1; if (_2sat(n,mid)) l=mid; else r=mid; } printf("%.2f\n",sqrt(l)*0.5); } return 0; }
Program-----vector存图..直接浮点进行运算...
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> #define ll long long #define eps 1e-5 #define oo 1000000007 #define pi acos(-1.0) #define MAXN 205 using namespace std; struct node { int x0,y0,x1,y1; }b[MAXN]; vector<int> T[MAXN]; int dfn[MAXN],low[MAXN],tp[MAXN],tpnum,DfsIndex; double arc[MAXN][MAXN]; bool instack[MAXN]; stack<int> mystack; double dis(int x0,int y0,int x1,int y1) { return sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)); } void tarjan(int x) { int i,y,m=T[x].size(); dfn[x]=low[x]=++DfsIndex; instack[x]=true; mystack.push(x); for (i=0;i<m;i++) { y=T[x][i]; if (!dfn[y]) { tarjan(y); low[x]=min(low[x],low[y]); }else if (instack[y]) low[x]=min(low[x],dfn[y]); } if (dfn[x]==low[x]) { tpnum++; do { x=mystack.top(); mystack.pop(); instack[x]=false; tp[x]=tpnum; }while (dfn[x]!=low[x]); } return; } bool _2sat(int n,double d) { int i,j; for (i=0;i<(n<<1);i++) T[i].clear(); for (i=0;i<n;i++) for (j=i+1;j<n;j++) { if (arc[i<<1][j<<1]+eps<d) T[i<<1].push_back(j<<1|1),T[j<<1].push_back(i<<1|1); if (arc[i<<1][j<<1|1]+eps<d) T[i<<1].push_back(j<<1),T[(j<<1)|1].push_back(i<<1|1); if (arc[i<<1|1][j<<1]+eps<d) T[(i<<1)|1].push_back((j<<1)|1),T[j<<1].push_back(i<<1); if (arc[i<<1|1][j<<1|1]+eps<d) T[(i<<1)|1].push_back(j<<1),T[(j<<1)|1].push_back(i<<1); } memset(dfn,0,sizeof(dfn)); memset(instack,false,sizeof(instack)); while (!mystack.empty()) mystack.pop(); tpnum=DfsIndex=0; for (i=0;i<(n<<1);i++) if (!dfn[i]) tarjan(i); for (i=0;i<n;i++) if (tp[i<<1]==tp[i<<1|1]) return false; return true; } int main() { int n,i; while (~scanf("%d",&n)) { for (i=0;i<n;i++) scanf("%d%d%d%d",&b[i].x0,&b[i].y0,&b[i].x1,&b[i].y1); for (i=0;i<n;i++) for (int j=i+1;j<n;j++) { arc[i<<1][j<<1]=arc[j<<1][i<<1]=dis(b[i].x0,b[i].y0,b[j].x0,b[j].y0); arc[i<<1][j<<1|1]=arc[j<<1|1][i<<1]=dis(b[i].x0,b[i].y0,b[j].x1,b[j].y1); arc[i<<1|1][j<<1]=arc[j<<1][i<<1|1]=dis(b[i].x1,b[i].y1,b[j].x0,b[j].y0); arc[i<<1|1][j<<1|1]=arc[j<<1|1][i<<1|1]=dis(b[i].x1,b[i].y1,b[j].x1,b[j].y1); } double l,r,mid; l=0,r=2*oo; while (r-l>eps) { mid=(r+l)/2; if (_2sat(n,mid)) l=mid; else r=mid; } printf("%.2lf\n",l*0.5); } return 0; }