zoukankan      html  css  js  c++  java
  • [luoguP2886] [USACO07NOV]牛继电器Cow Relays(矩阵)

    传送门

    矩阵快速幂,本质是floyd

    把 * 改成 + 即可

    注意初始化

    因为只有100条边,所以可以离散化

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define N 101
    #define min(x, y) ((x) < (y) ? (x) : (y))
    
    int n, t, s, e, cnt;
    int x[N], y[N], z[N], a[N];
    
    struct Matrix
    {
    	int n, m;
    	int a[N][N];
    	Matrix()
    	{
    		n = m = 0;
    		memset(a, 63, sizeof(a));
    	}
    };
    
    inline Matrix operator * (Matrix x, Matrix y)
    {
    	int i, j, k;
    	Matrix ans;
    	ans.n = x.n;
    	ans.m = y.m;
    	for(i = 1; i <= x.n; i++)
    		for(j = 1; j <= y.m; j++)
    			for(k = 1; k <= y.n; k++)
    				ans.a[i][j] = min(ans.a[i][j], x.a[i][k] + y.a[k][j]);
    	return ans;
    }
    
    inline Matrix operator ^ (Matrix x, int y)
    {
    	int i;
    	Matrix ans;
    	ans.n = ans.m = cnt;
    	for(i = 1; i <= cnt; i++) ans.a[i][i] = 0;
    	for(; y; y >>= 1)
    	{
    		if(y & 1) ans = ans * x;
    		x = x * x;
    	}
    	return ans;
    }
    
    int main()
    {
    	int i;
    	Matrix sum;
    	scanf("%d %d %d %d", &n, &t, &s, &e);
    	a[++cnt] = s;
    	a[++cnt] = e; 
    	for(i = 1; i <= t; i++)
    	{
    		scanf("%d %d %d", &z[i], &x[i], &y[i]);
    		a[++cnt] = x[i];
    		a[++cnt] = y[i];
    	}
    	std::sort(a + 1, a + cnt + 1);
    	cnt = std::unique(a + 1, a + cnt + 1) - (a + 1);
    	sum.n = sum.m = cnt;
    	for(i = 1; i <= t; i++)
    	{
    		x[i] = std::lower_bound(a + 1, a + cnt + 1, x[i]) - a;
    		y[i] = std::lower_bound(a + 1, a + cnt + 1, y[i]) - a;
    		sum.a[x[i]][y[i]] = z[i];
    		sum.a[y[i]][x[i]] = z[i];
    	}
    	sum = sum ^ n;
    	s = std::lower_bound(a + 1, a + cnt + 1, s) - a;
    	e = std::lower_bound(a + 1, a + cnt + 1, e) - a;
    	printf("%d
    ", sum.a[s][e]);
    	return 0;
    }
    

      

  • 相关阅读:
    3.18 每日一练
    第二章 练习
    第一章 练习
    Redis常用操作大全和Python操作Redis
    vue学习【第七篇】:Vue之导入Bootstrap
    Vue学习【第六篇】:Vue-cli脚手架(框架)与实战案例
    Redis 安装,配置以及数据操作
    vue学习【第五篇】:Vue组件
    vue学习【第三篇】:vue之node.js的简单介绍
    Vue学习【第二篇】:ES6简单介绍
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7492803.html
Copyright © 2011-2022 走看看