poj-1548-Robots- 贪心+模拟 / 最小路径覆盖
来源:互联网 发布:中国大国沙文主义 知乎 编辑:程序博客网 时间:2024/04/29 21:52
给你一个地图,给出部分格子告诉你是有钻石的。
然后你要每次从x=1,y=1处派出一个机器人,然后每次只能向右或向下走,问最少多少个机器人可以把所有钻石拿掉。
直接贪心,每次走最外面(就是尽量走最靠近右上角的点)的一条路即可
也即:在(x,y)的时候,看 第i行的 y+1到24还有没钻石,如果有就 往右走,如果没就往下走,当x或y==25就结束。
这样暴力模拟就n^2复杂度咯,每次查询 y+1到24列是否还有钻石要logn(用了树状数组) n^2logn咯
还有种方法就是 直接给所有点按照坐标序建图,,对于每个点x,显然能到达位于他的右下方的所有点v,给x和v加一条边。 最后跑一个最小路径覆盖也能解决咯
这个复杂度就变成了o(点数^2)
最糟糕就是 大约600个点咯。。点^2还是能接受的
贪心:
<pre name="code" class="cpp">#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <queue>#include <map>#include <set>#include <vector>using namespace std; int tm[30][30]; int tree[30][30]; int lowbit(int x){return x&-x;}void add(int x,int value,int num){for (int i=x;i<=24;i=i+lowbit(i)){tree[num][i]+=value;}}int get(int x,int num){int sum=0;for (int i=x;i;i-=lowbit(i)){sum+=tree[num][i];}return sum;}int main(){int i,j;int x,y;while(scanf("%d%d",&x,&y)!=EOF){if (x==-1&&y==-1)break;int cun=0;memset(tm,0,sizeof(tm)); memset(tree,0,sizeof(tree)); if (x==0&&y==0) {printf("0\n");continue;}tm[x][y]=1;cun++; while(scanf("%d%d",&x,&y)!=EOF){if (!x&&!y)break;tm[x][y]=1;cun++;}for (i=1;i<=24;i++) {for (j=24;j>=1;j--) {if (tm[i][j])add(j,1,i); }}int ans=0;while(1){if (cun==0)break;ans++;int xx=1,yy=1;while(1){if (tm[xx][yy]==1) {tm[xx][yy]=0; add(yy,-1,xx);cun--;} if (xx==24&&yy==24)break;int ret=get(24,xx)-get(yy,xx);if (ret>0) yy++;elsexx++;if (xx==25) break;//再也无法得到钻石if (yy==25) break;//再也无法得到钻石}}printf("%d\n",ans);}return 0;}
最小路径覆盖:
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <map>#include <set>#include <vector>#include <iostream>using namespace std;const double pi=acos(-1.0);double eps=0.000001; //顶点编号从0开始的const int MAXN = 628;int uN,vN;//u,v的数目,使用前面必须赋值int g[MAXN][MAXN];//邻接矩阵int linker[MAXN];bool used[MAXN];bool dfs(int u){for(int v = 0; v < vN;v++)if(g[u][v] && !used[v]){used[v] = true;if(linker[v] == -1 || dfs(linker[v])){linker[v] = u;return true;}}return false;}int hungary(){int res = 0; memset(linker,-1,sizeof(linker));for(int u = 0;u < uN;u++){memset(used,false,sizeof(used));if(dfs(u))res++;}return res;}struct node{int x,y;int id;node(){}node (int a,int b,int c){x=a,y=b;id=c;} };/*bool cmp (const node& a,const node& b) {if (a.y!=b.y)return a.y<b.y;return a.x<b.x;} */vector <node> mp; int main(){int i,j,k;int x,y;while(scanf("%d%d",&x,&y)!=EOF){ int num=0;if (x==-1&&y==-1)break; memset(g,0,sizeof(g));mp.clear();if (x==0&&y==0) {printf("0\n");continue;}mp.push_back(node(x,y,++num)); while(scanf("%d%d",&x,&y)!=EOF){if (!x&&!y)break;mp.push_back(node(x,y,++num)); } for (j=0;j<mp.size();j++){int x=mp[j].id-1;for (k=0;k<mp.size();k++){if (j==k) continue;if (mp[j].x>mp[k].x||mp[j].y>mp[k].y) continue;if (mp[j].x==mp[k].x&&mp[j].y>mp[k].y)continue;if (mp[j].y==mp[k].y&&mp[j].x>mp[k].x)continue;int v=mp[k].id-1;g[x][v]=1;}} uN=vN=num;int ans=hungary();printf("%d\n",num-ans);}return 0;}
0 0
- poj-1548-Robots- 贪心+模拟 / 最小路径覆盖
- POJ 1548 Robots(DAG最小路径覆盖)
- poj 1548 Robots 最小路径覆盖
- POJ 1548 Robots(最小路径覆盖)
- poj 1548 Robots(最小路径覆盖)
- POJ 1548 Robots(DAG最小路径覆盖)
- poj 1548 Robots 最小路径覆盖
- POJ-1548-Robots(最小路径覆盖)
- POJ 1548 Robots(DAG最小路径覆盖)
- POJ - 1548 Robots 二分图 最小路径覆盖
- Poj 1548 Robots【最小路径覆盖---二分匹配】
- 【二分图+最小路径覆盖+建图难度】北大 poj 1548 Robots
- poj 1548 Robots 最小路径匹配解法
- POJ 1548 最小路径覆盖
- poj 1548(最小路径覆盖)
- poj 1548最小路径覆盖
- POJ1548 Robots【二分图最小路径覆盖】
- poj 2060(最小路径覆盖)
- java 多线程( Future Callable)
- 微信(WeChat web page)
- RHEL 7.0 yum源
- pixhawk上的 SPKT/DSM接口介绍
- 在VMware Workstation 12 PRO上安装CentOS 7 Minimal版
- poj-1548-Robots- 贪心+模拟 / 最小路径覆盖
- poj2438 哈密顿图
- think python学习心得-(2)接口设计
- java第九天
- HDOJ 1092 A+B for Input-Output Practice (IV)
- Earthstone: Easy Version
- WEB-laydate兼容bootstrap样式错误
- 解决数据库文件导入时出现的#1046 - No database selected错误。
- 42. Trapping Rain Water