hdu5365Shortest Path (floyd)
来源:互联网 发布:fastjson与json 编辑:程序博客网 时间:2024/05/19 11:49
Problem Description
There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 to n and there is an edge with unit length between i and i+1 (1≤i<n) . To make the graph more interesting, someone adds three more edges to the graph. The length of each new edge is 1 .
You are given the graph and several queries about the shortest path between some pairs of vertices.
You are given the graph and several queries about the shortest path between some pairs of vertices.
Input
There are multiple test cases. The first line of input contains an integer T , indicating the number of test cases. For each test case:
The first line contains two integern and m (1≤n,m≤105) -- the number of vertices and the number of queries. The next line contains 6 integers a1,b1,a2,b2,a3,b3 (1≤a1,a2,a3,b1,b2,b3≤n) , separated by a space, denoting the new added three edges are (a1,b1) , (a2,b2) , (a3,b3) .
In the nextm lines, each contains two integers si and ti (1≤si,ti≤n) , denoting a query.
The sum of values ofm in all test cases doesn't exceed 106 .
The first line contains two integer
In the next
The sum of values of
Output
For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7) , where zi is the answer for i -th query.
Sample Input
110 22 4 5 7 8 101 53 1
Sample Output
7
题意:给你一条n个点组成的链,相邻两点的距离为1,再给你三条边,这三条边的端点都是链上的点,且每一条的距离为1。有m个询问,问你对于每两个点,从一个端点到另一个端点的最近距离是多少。
思路:可以先初始化3条边中6个点两两之间的最短距离,这个可以用floyd做,那么对于每一个询问,两个点x1,x2的最短距离为不经过任何点,或者经过3条边中的某些边,又因为我们已经初始化出3条边中任意两个点的最短距离,所以我们只要枚举a,b,即x1到a,a到b,再b到x2的最近距离。这一题floyd初始化时关键,如果每次直接8个点floyd时间复杂度就爆了。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<bitset>#include<algorithm>using namespace std;typedef long long ll;typedef long double ldb;#define inf 1000000007#define pi acos(-1.0)#define MOD 1000000007int dist[10][10];void floyd(){ int i,j,k; for(k=1;k<=6;k++){ for(i=1;i<=6;i++){ for(j=1;j<=6;j++){ if(dist[i][j]>dist[i][k]+dist[k][j]){ dist[i][j]=dist[i][k]+dist[k][j]; } } } }}int main(){ int n,m,i,j,T,k; int x[10]; int a1,b1,a2,b2,a3,b3; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6]); for(i=1;i<=6;i++){ for(j=1;j<=6;j++){ dist[i][j]=abs(x[i]-x[j]); } } dist[1][2]=dist[2][1]=min(dist[1][2],1); dist[3][4]=dist[4][3]=min(dist[3][4],1); dist[5][6]=dist[6][5]=min(dist[5][6],1); floyd(); ll sum=0; for(k=1;k<=m;k++){ scanf("%d%d",&x[7],&x[8]); int ans=abs(x[7]-x[8]); for(i=1;i<=6;i++){ for(j=1;j<=6;j++){ ans=min(ans,abs(x[7]-x[i] )+abs(x[8]-x[j])+dist[i][j] ); ans=min(ans,abs(x[7]-x[j] )+abs(x[8]-x[i])+dist[i][j] ); } } sum=(sum+(ll)ans*(ll)k)%MOD; //printf("%d\n",floyd()); } printf("%lld\n",sum); } return 0;}
0 0
- hdu5365Shortest Path (floyd)
- floyd + path
- HDU3631:Shortest Path(Floyd)
- HDU3631 Shortest Path 【Floyd】
- HDU3631:Shortest Path(Floyd)
- short-path problem (Floyd)
- hdu2807The Shortest Path floyd
- HDU5636 -- Shortest Path -- floyd
- HDU2807:The Shortest Path(Floyd)
- 【HDU】3631 Shortest Path 【floyd】
- hdu 3631 Shortest Path (floyd)
- HDU 5636:Shortest Path floyd
- HDU 5636:Shortest Path【Floyd】
- hdu 3631 Shortest Path【Floyd】
- hdu 3631 Shortest Path (floyd)
- HDU 3631 Shortest Path Floyd
- POJ 3631 SHORTEST PATH(FLOYD改装)
- hdu 3631 Shortest Path(floyd插点法)
- HDU 2719 The Seven Percent Solution (printf输出%25)
- 母牛生小牛
- Linux下锐捷客户端v1.31安装与登陆
- Python与自然语言处理(一)搭建环境
- ListView与SimpleAdapter
- hdu5365Shortest Path (floyd)
- jstack dump 线程说明
- 解析xml dom4j
- 【权限管理】基础原理篇
- 教你打造广告位的循环播放
- BZOJ3668 NOI2014 起床困难综合症 题解&代码
- CDOJ 1091 秋实大哥の恋爱物语 KMP
- BZOJ_P3450&Tyvj_P1952 Easy(概率期望动态规划)
- centos6.5有线网卡编译安装