【BZOJ1210】【HNOI2004】邮递员

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/79978920

【题目链接】

【思路要点】

  • 基于连通性的的动态规划模板题。
  • 时间复杂度\(O(3^M*N*M)\),实现时需要手写一个小高精。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	30
#define TMPN	15
#define HASH	1000005
#define P	100003
#define Q	10000000000000000
struct Extended_LL {long long a, b; };
Extended_LL operator + (Extended_LL x, Extended_LL y) {
	Extended_LL c = {x.a + y.a, x.b + y.b};
	c.a += c.b / Q; c.b %= Q;
	return c;
}
struct Hash_Table {
	int head[P], nxt[HASH];
	int curr[HASH], size;
	Extended_LL value[HASH];
	void clear() {
		size = 0;
		memset(head, 0, sizeof(head));
	}
	void modify(int x, Extended_LL val) {
		int pos = x % P;
		for (int i = head[pos]; i; i = nxt[i])
			if (curr[i] == x) {
				value[i] = value[i] + val;
				return;
			}
		size++;
		curr[size] = x;
		value[size] = val;
		nxt[size] = head[pos];
		head[pos] = size;
	}
};
Hash_Table ans[2];
int n, m, ex, ey, num[MAXN], bit[MAXN], all;
Extended_LL finalans;
void Extend(int x, int y, int curr, int dest, Extended_LL val) {
	int tmpy = num[y] & curr;
	int tmpx = num[y - 1] & curr;
	int typey = 0, typex = 0;
	if (tmpy) {
		if (tmpy & (1 << 2 * y)) typey = 1;
		else typey = 2;
	}
	if (tmpx) {
		if (tmpx & (1 << 2 * (y - 1))) typex = 1;
		else typex = 2; 
	}
	if (x == ex && y == ey) {
		int tcurr = curr ^ tmpy ^ tmpx;
		if (tcurr == 0 && typex == 1 && typey == 2) finalans = finalans + val;
		return;
	}
	if (tmpy == 0 && tmpx == 0) {
		if (y == m) return;
		else {
			ans[dest].modify(curr | (1 << 2 * (y - 1)) | (2 << 2 * y), val);
			return;
		}
	}
	if (tmpy != 0 && tmpx != 0) {
		int tcurr = curr ^ tmpx ^ tmpy;
		if (typex == 1 && typey == 2) return;
		if (typex == 2 && typey == 1) {
			if (y != m) {
				ans[dest].modify(tcurr, val);
				return;
			} else {
				ans[dest].modify(tcurr << 2, val);
				return;
			}
		}
		static int type[MAXN], q[MAXN], tnp[MAXN], pir[MAXN];
		int top = 0;
		for (int i = 0; i <= m; i++) {
			tnp[i] = curr & num[i];
			type[i] = 0;
			if (tnp[i]) {
				if (tnp[i] & (1 << 2 * i)) type[i] = 1;
				else type[i] = 2;
			}
		}
		for (int i = 0; i <= m; i++) {
			if (type[i] == 1) q[++top] = i;
			if (type[i] == 2) {
				pir[q[top]] = i;
				pir[i] = q[top];
				top--;
			}
		}
		if (typex == 1 && typey == 1) {
			ans[dest].modify(tcurr ^ tnp[pir[y]] ^ (1 << 2 * pir[y]), val);
			return;
		} else {
			if (y != m) {
				ans[dest].modify(tcurr ^ tnp[pir[y - 1]] ^ (2 << 2 * pir[y - 1]), val);
				return;
			} else {
				ans[dest].modify((tcurr ^ tnp[pir[y - 1]] ^ (2 << 2 * pir[y - 1])) << 2, val);
				return;
			}
		}
	}
	if (tmpy == 0) {
		int tcurr = curr ^ tmpx;
		if (y != m) ans[dest].modify(curr, val);
		else ans[dest].modify(curr << 2, val);
		if (y != m) ans[dest].modify(tcurr | (typex << 2 * y), val);
	} else {
		int tcurr = curr ^ tmpy;
		if (y != m) ans[dest].modify(curr, val);
		if (y != m) ans[dest].modify(tcurr | (typey << 2 * (y - 1)), val);
		else ans[dest].modify((tcurr | (typey << 2 * (y - 1))) << 2, val);
	}
}
void work(int x, int y, int now) {
	int tx = x, ty = y + 1;
	if (ty > m) ty = 1, tx++;
	ans[now ^ 1].clear();
	for (int i = 1; i <= ans[now].size; i++)
		Extend(x, y, ans[now].curr[i], now ^ 1, ans[now].value[i]);
	if (x == ex && y == ey) return;
	else work(tx, ty, now ^ 1);
}
void print(Extended_LL x) {
	if (x.a) {
		printf("%lld", x.a);
		long long now = Q / 10;
		while (now) {
			printf("%lld", x.b / now);
			x.b %= now;
			now /= 10;
		}
		printf("\n");
	} else printf("%lld\n", x.b);
}
int main() {
	scanf("%d%d", &m, &n);
	if (n == 1 || m == 1) {
		printf("1\n");
		return 0;
	}
	num[0] = 3; bit[0] = 1;
	for (int i = 1; i <= m + 1; i++)
		num[i] = num[i - 1] << 2;
	for (int i = 1; i < MAXN; i++)
		bit[i] = bit[i - 1] << 1;
	ex = n, ey = m;
	if (ex == 0) printf("0\n");
	else {
		ans[0].clear();
		ans[1].clear(); 
		ans[0].modify(0, (Extended_LL) {0, 1});
		finalans = (Extended_LL) {0, 0};
		work(1, 1, 0);
		finalans = finalans + finalans;
		print(finalans);
	}
	return 0;
}

邮递员投递问题

10-07

[code=C/C++]rn/*rn题目描述:rn有一个邮递员要在n个城市之间来回送信。但有的城市之间有大路相连rn而有的没有路。现在要由一个城市到另一个城市送信,中途最少要经过rn多少个其它的城市呢?rnrn 输入:rn 第一行是n,k(1<=n<=10000, 1<=k<=20000),接下来就是k行。rn 这k行每行有两个数a,b(1 <= a,b <= n),表示城市a和b之间有大路rn k行以后就是两个数p和q。rn 当n,k输入都为0的时候结束。rn rn 输出:rn 输出从城市p到城市q之间最少要经过的其它的城市的数目。rn 如果p和q之间不连通则输出"No solution"rn rn 样例输入:rn 6 6rn 1 4rn 1 2rn 2 3rn 3 4rn 5 4rn 5 6rn 1 6rn 0 0rn rn 样例输出:rn 2rn*/rnrnrn#include rnusing namespace std;rnrn#define MAX 10rnrn/*结点代表城市.stage为城市标号,num为与该城市相连的rn其它城市个数,link数组为与该城市相连的城市的标号*/rnstruct Nodernrn int stage;rn int num;rn int mark;rn int link[MAX];rn Node()rn rn stage=0;rn num=0;rn mark=0;rn int link[MAX]=0;rn rn;rnrnNode node[MAX+1];rnrnint findway(Node sta,Node des);rnrnint main()rnrn /*建立结点之间的关系*/rn int k;rn cin>>k;rn int a,b,p1=0,p2=0;rn for(int i=0;i!=k;i++)rn rn cin>>a>>b;rn for(int j=0;j<=node[0].num;j++)rn rn if(a==node[j].stage)rn rn p1=j;rn continue;rn rn if(b==node[j].stage)rn rn p2=j;rn continue;rn rn rn if(p1==0)rn rn p1=++node[0].num;rn node[p1].stage=a;rn rn if(p2==0)rn rn p2=++node[0].num;rn node[p2].stage=b;rn rn node[p1].link[node[p1].num++]=b;rn node[p2].link[node[p2].num++]=a;rn p1=p2=0;rn rn /*输出检测结点是否建立正确*/rn for(i=1;i<=node[0].num;i++)rn rn cout<>start>>dest;rn for(i=0;i!=k;i++)rn rn for(int j=0;j<=node[0].num;j++)rn rn if(start==node[j].stage)rn rn p1=j;rn continue;rn rn if(dest==node[j].stage)rn rn p2=j;rn continue;rn rn rn rn length=findway(node[p1],node[p2]);rn cout<

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试