Daliy Algorithm (图论,位运算,思维)-- day 57

Nothing to fear

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.4.12


最短路变短了

思路 :
我们会发现1 - n 的路径上如果切换了
可以转化成为 len(1-n) = len(1-i) + len(i - j) + len(j - n);
如果我们反转了任意一条边的话实际上知识反转了i - j之间并不影响从
1-i 和 j-n之间的最短距离

这是时候我们不可能每次切换一次都求一次从 j-n的最短路 我们可以逆向
求出 n-每个一点的但单源最短路径

注意再求答案的时候要将边取反
即原本路径是从 i ---> j
由于我们只需要计算出原本的len(1-i) + len(i-j) + len(j-n);
此时由于边取反 我们的 i j 互换
len(1-n) = len(1-j) + len(j-i) + len(i-n);

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <queue>

using namespace std;

typedef long long ll;

const int N = 100005;
const int M = 200005;

struct edge
{
	int to, dis , next;
}e[M] ,re[M];

ll head[N], dis[N], cnt, n , m ,s; // 正向
ll rhead[N] , rdis[N], rcnt; // 反向存储 
bool vis[N] , rvis[N];

struct Edge{
	int a, b, c;
}map[M];

void add(int a,int b,int v)
{
	cnt++;
	e[cnt].dis = v;
	e[cnt].to = b;
	e[cnt].next = head[a];
	head[a] = cnt;

	// 反向存边
	rcnt++;
	re[rcnt].to = a;
	re[rcnt].dis = v;
	re[rcnt].next = rhead[b];
	rhead[b] = rcnt; 
}

struct node{
	ll dis , pos;
	bool operator < (const node &x)const 
	{
		return x.dis < dis;
	}
};


void dijkstra(int s)
{
	priority_queue<node> q;
	memset(dis , 0x3f , sizeof dis);
	dis[s] = 0;
	q.push((node){0,s});
	while(!q.empty())
	{
		node tmp = q.top();
		q.pop();
		int x = tmp.pos;
		if(vis[x])continue;
		vis[x] = 1;
		for(int i = head[x]; i ;i = e[i].next)
		{
			int y = e[i].to;
			if(dis[y] > dis[x] + e[i].dis)
			{
				dis[y] = dis[x] + e[i].dis;
				if(!vis[y])
				{
					q.push((node){dis[y],y});
				}
			}
		}
	}
}

void rdijkstra(int s)
{
	priority_queue<node> q;
	memset(rdis , 0x3f , sizeof rdis);
	rdis[s] = 0;
	q.push((node){0,s});
	while(!q.empty())
	{
		node tmp = q.top();
		q.pop();
		int x = tmp.pos;
		if(rvis[x])continue;
		rvis[x] = 1;
		for(int i = rhead[x]; i ;i = re[i].next)
		{
			int y = re[i].to;
			if(rdis[y] > rdis[x] + re[i].dis)
			{
				rdis[y] = rdis[x] + re[i].dis;
				if(!rvis[y])
				{
					q.push((node){rdis[y],y});
				}
			}
		}
	}
}

void init()
{
	cin >> n >> m;
	int a , b , c;
	for(int i = 1;i <= m ;i ++)
	{
		scanf("%d %d %d",&a ,& b,&c);
		add(a , b , c);
		map[i] = {a , b , c}; // 存边
	}
}
// 查询某条边的路径是否变短
void query(int x)
{
	int s = map[x].a;
	int e = map[x].b;
	int v = map[x].c;
	// 边取反的过程
	ll t = dis[e] + rdis[s] + v;
	cout << t << " " << dis[n] << endl;
	if(t < dis[n])cout << "YES\n";
	else cout << "NO\n";
}
int main()
{
	init();
	dijkstra(1);
	rdijkstra(n);
	
	for(int i = 1;i <= n ;i ++)
	{
		cout << dis[i] << " ";
	}cout << endl;
	for(int i = 1;i <= n; i++)
	{
		cout << rdis[i] << " ";
	}
	cin >> s;
	int x = 0;
	while(s--)
	{
		scanf("%d" , & x);
		query(x);
	}
	return 0;
}

大吉大利

考察推理观察和位运算 QWQ

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;
typedef long long ll;
const int N = 100005;
int n, a[N] ,b[32];
ll cnt = 0;
int main()
{
	cin >> n;
	for(int i = 1;i <= n ;i ++)
	{
		scanf("%d",&a[i]);
	}
	ll sum = 0;
	for(int i = 1;i <= n ;i ++)
	{
		int t = a[i] , j = 0;
		while(t){
			b[j++] += (t & 1) ;
			t >>= 1;
		}
	}
	for(int i = 0;i < 31; i++)
	{
		sum += (ll)b[i] * b[i] * (1 << i);
	}
	cout << sum << endl;
	return 0;
}

三角形周长

一开始也猜到了这么做但是为什么不用判断周长是否成立呢

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 10005;
const int mod = 998244353;

struct node{
	ll x, y;
}a[N];
int n;

// 计算两点之间的曼哈顿距离
// 注意这里用int 是存在溢出的
ll getdis(ll sx,ll sy,ll ex,ll ey)
{
	return (abs(sx - ex) + abs(sy - ey)) % mod;
}
int main()
{
	cin >> n;
	for(int i = 1;i <= n ;i ++)
	{
		scanf("%lld %lld",&a[i].x,&a[i].y);
	}
	int t = n - 2;
	ll sum = 0;
	for(int i = 1;i <= n ;i ++)
	{
		for(int j = i + 1;j <= n ;j ++)
		{
			sum = (sum +  t * getdis(a[i].x,a[i].y,a[j].x,a[j].y)) % mod;
		}
	}
	cout << sum << endl;
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12689727.html