HDU 5636
来源:互联网 发布:武汉ui培训知乎 编辑:程序博客网 时间:2024/06/07 03:23
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5636
Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 335 Accepted Submission(s): 105
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
进行m次询问,每次给出a,b,求从a到b的最小代价
把三个传送门给的6个点当成节点做一次 floyd,然后每次把6个节点遍历一遍即可...
上面是题解...复杂度为o(6^2*m),讲道理啊。。我写了一个o(6^3*m)就超时了,有必要卡那么严格吗╮(╯_╰)╭
然后dmax一开始赋值为x-y的绝对值,结果还没输入就被赋值了。。结果样例还看不出来。。改了半小时。。。
下面代码
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<iostream>#include<algorithm>#include<functional>#include<queue>#include<vector>#include<set>#include<map>using namespace std;__int64 qfabs(__int64 n){if(n<0) return -n;return n;}__int64 dmap[20][20];int main(){int nn,ii,i,j,k,m,n;__int64 sum,dmax,ans,x,y,a[10],b[10];scanf("%d",&nn);__int64 l=1e+9+7;while(nn--){ans=0;memset(a,0,sizeof(a));memset(dmap,0,sizeof(dmap));scanf("%d%d",&m,&n);for(i=1;i<=3;i++){scanf("%I64d%I64d",&a[i],&b[i]);a[i+3]=b[i];}for(i=1;i<=6;i++)for(j=1;j<=6;j++)dmap[i][j]=qfabs(a[i]-a[j]);for(i=1;i<=3;i++)dmap[i][i+3]=dmap[i+3][i]=1;for(k=1;k<=6;k++)for(i=1;i<=6;i++)for(j=1;j<=6;j++)if(dmap[i][j]>dmap[i][k]+dmap[k][j]) dmap[i][j]=dmap[i][k]+dmap[k][j];for(ii=1;ii<=n;ii++){scanf("%I64d%I64d",&x,&y);dmax=qfabs(x-y);for(i=1;i<=6;i++)for(j=1;j<=6;j++){sum=qfabs(a[i]-x)+dmap[i][j]+qfabs(a[j]-y);if(sum<dmax) dmax=sum;}ans=(ans+(dmax*ii))%l;}printf("%I64d\n",ans);}return 0;}
0 0
- HDU 5636
- Bestcoder #74 hdu 5635 hdu 5636 hdu 5637 hdu 5638
- HDU 5636 Shortest Path
- hdu 5636(最短路)
- hdu 5636 Shortest Path
- HDU 5636 Shortest Path
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- 单链表(Singly Linked List)
- 按照艺术家分组并输出某个艺术家的唯一键-值对
- 浙江省省赛[扩展欧几里得] J Modular Inverse
- Java你可能不知道的事系列1
- 代理模式Proxy入门 之帮你追求心仪的姑娘
- HDU 5636
- 四柱汉诺塔
- GC知识要点回顾
- 利用艺术家的整数ID映射将标签转换为向量
- selector修改TextView中字体的颜色
- 组合部分标签向量并累加成完整向量
- Qt QProgressBar 重写前的练习,了解Qt 槽函数的线程机制
- 使用excel来快捷计算日期的方法
- javascript--判空判等