线性基【CF845G】Shortest Path Problem?
Description
给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\)
每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权
如果在点 \(n\),你可以选择结束游戏
求一种方案,使得结束游戏后价值最小
\(n,m \le 10^5\)
Input
第一行为两个整数\(n,m\)代表有\(n\)个点\(m\)条边。
接下来\(m\)行,描述一条从\(x\)到\(y\)长度为\(z\)的无向边。
Output
一个整数,代表最小价值。
首先,很明确的一点,题目要求我们求出很多条边的最小异或和。
由此,我们可以想到线性基
由于我们可以重复经过一些边,那么根据异或性质,当这条边被重复走过两次,那它对答案的贡献就是\(0\)。但是即使这样,它还可能连向其他的点。
虽然我们没办法枚举边,但是可以考虑将这些边所在分为两种。
- 环上的边
- 链上的边
但是我们通向一个环的时候会经过一条连向这个环的边两次。(一进一出)。
因此,我们考虑维护每个环的异或和,塞入线性基中。
再找一条链,去和其异或起来最小。即可。
这条链可以随便选择。
简单证明一下;
假设存在两条链\(A,B\),我们现在选择了不优的\(A\)链,但是在求解答案的时候(利用线性基)
我们会异或到一个环(\(A,B\)链围成的环),这时,就好比我们原路返回,又选择了较优的\(B\)链。
因此,这个题就解决了.
代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#define R register
#define lo long long 
using namespace std;
const int gz=1e5+8;
inline void in(R int &x)
{
    int f=1;x=0;char s=getchar();
    while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
    while(isdigit(s)){x=x*10+s-'0';s=getchar();}
    x*=f;
}
int head[gz<<1],tot;
struct cod{int u,v;lo w;}edge[gz<<2];
lo p[64],dis[gz];
inline void add(R int x,R int y,R lo z)
{
    edge[++tot].u=head[x];
    edge[tot].v=y;
    edge[tot].w=z;
    head[x]=tot;
}
int n,m;
bool vis[gz];
inline void ins(R lo x)
{
    for(R int i=63;i>=0;i--)
    {
        if((x>>i)&1LL)
        {
            if(p[i])
                x^=p[i];
            else
            {
                p[i]=x;
                break;
            }
        }
    }
}
inline lo query(R lo o)
{
    R lo res=o;
    for(R int i=63;i>=0;i--)
        if((res^p[i])<res)res^=p[i];
    return res;
}
void dfs(R int u,R lo now)
{
    vis[u]=true;dis[u]=now;
    for(R int i=head[u];i;i=edge[i].u)
    {
        if(!vis[edge[i].v])
            dfs(edge[i].v,now^edge[i].w);
        else ins(now^edge[i].w^dis[edge[i].v]);
    }
}
int main()
{
    in(n);in(m);
    for(R int i=1,x,y;i<=m;i++)
    {
        R lo z;
        in(x),in(y);
        scanf("%lld",&z);
        add(x,y,z),add(y,x,z);
    }
    dfs(1,0);
    printf("%lld\n",query(dis[n]));
}
线性基【CF845G】Shortest Path Problem?的更多相关文章
- [CF845G]Shortest Path Problem?
		题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ... 
- Codefroces Educational Round 27 845G Shortest Path Problem?
		Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ... 
- 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码
		00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ... 
- 【CF edu 27 G. Shortest Path Problem?】
		time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ... 
- Codeforces 845G Shortest Path Problem?
		http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ... 
- AT [ABC177F] I hate Shortest Path Problem
		因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路.可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([L, ... 
- Solve Longest Path Problem in linear time
		We know that the longest path problem for general case belongs to the NP-hard category, so there is ... 
- Why longest path problem doesn't have optimal substructure?
		We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ... 
- 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)
		[CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ... 
随机推荐
- LightOJ 1062 - Crossed Ladders 基础计算几何
			http://www.lightoj.com/volume_showproblem.php?problem=1062 题意:问两条平行边间的距离,给出从同一水平面出发的两条相交线段长,及它们交点到水平 ... 
- 2015/9/5 Python基础(9):条件和循环
			条件语句Python中的if语句如下: if expression: expr_true_suite 其中expression可以用布尔操作符and, or 和 not实现多重判断条件.如果一个复合语 ... 
- python字符串内置函数
			1.字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串特性:1.只能存放一个值2.不可变3.按照从左到右的顺序定义字符集合,下标 ... 
- 深入HBase架构解析(一)
			前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官网看到了这篇文文章:An In-Depth Look at the HBase Architecture,原本想翻译全文,然 ... 
- 元类编程-- __new__和__init__的区别
			class User: def __new__(cls, *args, **kwargs): print (" in new ") return super().__new__(c ... 
- 调戏OpenShift:一个免费能干的云平台(已失效)
			一.前因后果 以前为了搞微信的公众号,在新浪sae那里申请了一个服务器,一开始还挺好的 ,有免费的云豆送,但是一直运行应用也要消费云豆,搞得云豆也所剩无几了.作为一名屌丝,日常吃土,就单纯想玩一玩微信 ... 
- 「6月雅礼集训 2017 Day8」gcd
			[题目大意] 定义times(a, b)表示用辗转相除计算a和b的最大公约数所需步骤. 那么有: 1. times(a, b) = times(b, a) 2. times(a, 0) = 0 3. ... 
- 如果你也想写个完整的 Vue 组件项目
			1.一个完整的组件项目需要什么? 必要的: 组件构建方式 ( webpack / rollup 之类 ),并提供至少一个主流的输出格式 (ESModule) Demo 及 Demo 源码 文档,可以是 ... 
- Intel MKL(Math Kernel Library)
			1.Intel MKL简介 Intel数学核心函数库(MKL)是一套高度优化.线程安全的数学例程.函数,面向高性能的工程.科学与财务应用.英特尔 MKL 的集群版本包括 ScaLAPACK 与分布式内 ... 
- Java垃圾收集算法
			算法名称 过程 优缺点 1. 标记-清除算法 (Mark-Sweep) 分为两个阶段: 1.首先标记出所有需要回收的对象: 2.在标记完成后统一回收所有被标记的对象. 缺点: 1.效率问题:标记和清除 ... 
