poj3613 求经过n条边的最短路 ----矩阵玩出新高度 。
For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.
Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.
To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.
Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.
Input
* Line 1: Four space-separated integers: N, T, S, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i
Output
* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.
Sample Input
2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9
Sample Output
10
Source
http://blog.csdn.net/monster__yi/article/details/51069236 感谢题解
矩阵mx[i][j]表示已经有一条i->j的边,然后在和基础矩阵进行运算,那么mx[j][k],就代表再走一条边从i到j,满足了每次只走一条边的条件
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<int,int>mp;
int tot,k;
struct Martix{
int a[][];
Martix operator * (Martix &rhs){
Martix c;
memset(c.a,0x3f,sizeof(c.a));
for(int i=;i<=tot;++i)
for(int j=;j<=tot;++j)
for(int k=;k<=tot;++k)
c.a[i][j]=min(c.a[i][j],a[i][k]+rhs.a[k][j]);
return c;
}
}base,ans;
int main(){
int u,v,x,m,st,ed;
tot=;
memset(base.a,0x3f,sizeof(base.a));
scanf("%d%d%d%d",&k,&m,&st,&ed);
while(m--){
scanf("%d%d%d",&x,&u,&v);
if(!mp[u]) mp[u]=++tot;
if(!mp[v]) mp[v]=++tot;
base.a[mp[u]][mp[v]]=base.a[mp[v]][mp[u]]=x;
}
ans=base;
--k;
while(k){
if(k&) ans=ans*base,--k;
k>>=;
base=base*base;
}
printf("%d\n",ans.a[mp[st]][mp[ed]]);
}
poj3613 求经过n条边的最短路 ----矩阵玩出新高度 。的更多相关文章
- POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)
题意: 给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路: 感觉很赞的一个题目,据说证明是什 ...
- 经过N条边的最短路
http://acm.pku.edu.cn/JudgeOnline/problem?id=3613 求经过N条边的最短路 (2 ≤ N ≤ 1,000,000) 倍增floyd,主体是矩阵乘法.考虑一 ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- c编程:求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。
//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max, ...
- HDU 4396More lumber is required 过至少K条边的最短路
/* ** 题目要求过最少k条边的最短路 */ #include <iostream> #include <cstdio> #include <cstring> # ...
- Cow Relays,过N条边的最短路
题目链接 题意: 找从a到b的经过N条边的最短路 分析: 有点板子...方法:矩阵存,然后有个类似快速幂的思想,然后再加上离散化就好了. 没啥写的,只能说说矩阵了,我用的方法是先枚举i,j再枚举k,当 ...
- 求去掉一条边使最小割变小 HAOI2017 新型城市化
先求最小割,然后对残量网络跑Tarjan.对于所有满流的边,若其两端点不在同一个SCC中,则这条边是满足条件的. 证明见 来源:HAOI2017 新型城市化
- hive实现根据用户分组,按用户记录求上下两条记录的时间差
在mysql,数据如下:#查询某一用户该日抽奖时间 select draw_time from user_draw_log where user_id = 1 and draw_date='2016- ...
- POJ 2253:Frogger 求每一条路径最大值里面的最小值
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31490 Accepted: 10150 Descrip ...
随机推荐
- Linux改中文乱码显示
Linux改中文乱码显示 可以使用locale命令,查看当前系统默认采用的字符集# locale在RedHat/CentOS系统下,记录系统默认使用语言的文件是/etc/ ...
- 一个简易的SocketIM
今天做了一个简易的socketIM的小示例.基本思想是开启两个winform,每个winform既充当服务器也充当客户端.一个监听8000端口,另外一个监听8001端口,两个winform接收到信息之 ...
- CF786B Legacy(线段树优化建边)
模板题CF786B Legacy 先说算法 如果需要有n个点需要建图 给m个需要建边的信息,从单点(或区间内所有点)向一区间所有点连边 如果暴力建图复杂度\(mn^2\) 以单点连向区间为例,在n个点 ...
- 关于【MySQL 子查询——查询最大值】的补充说明
昨天在使用子查询查找最高分和最低分时遇上了一点问题,情况是这样的:如果找到的最高分或最低分是唯一值则不会有什么问题,但如果有其它班级学生的成绩恰好与查询的最高分或最低分相同时就会把那个学生的信息也显示 ...
- MAC使用vagrant搭建开发环境
公司的开发环境是这样的: Windows主机通过虚拟机安装CentOS.平时在Windows下编辑代码,然后跑到虚拟机里编译. 我自己有台MAC,本来准备直接在MAC上装开发环境的.基于以下两个原因放 ...
- oracle常用字符函数
字符函数: concat:(字符连接函数) --字符连接 select concat('con','cat') from dual; select 'co'||'nc'||'at' from dual ...
- RSA host key has changed
- 13_JavaScript基础入门(3)
条件分支语句 条件分支语句,也叫作条件判断语句,就是根据某种条件执行某些语句,不执行某些语句. JS中有三种语法是可以表示条件分支的. 1.if--else-- 条件分支的主力语法,这个主力语法能够书 ...
- HTTP接口测试
HTTP接口测试 1.1 get接口 请求URL http://api.nnzhp.cn/api/user/stu_info 请求方式 get 请求参数 参数名 必选 类型 说明 stu_name 是 ...
- LeetCode--LinkedList--83.Remove Duplicates from Sorted List(Easy)
题目地址https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 83. Remove Duplicates from Sor ...