HDU 5636 Shortest Path
来源:互联网 发布:linux部署环境 编辑:程序博客网 时间:2024/06/08 09:01
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题解说的是用floyd,我是直接dfs,常数上差了点,原来在bc里面还能过的,现在外面过不了了。#include<cstdio>#include<string>#include<cstring>#include<vector>#include<iostream>#include<queue>#include<map>#include<algorithm>using namespace std;typedef long long LL;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 1e5 + 10;int T, n, m, s, t, ans, res;int a[6], f[6];void dfs(int s, int dis){ ans = min(ans, dis + abs(s - t)); for (int i = 0; i < 6; i++) if (f[i]) { f[i] = f[i ^ 1] = 0; dfs(a[i ^ 1], dis + abs(s - a[i]) + 1); f[i] = f[i ^ 1] = 1; }}int main(){ scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 0; i < 6; i++) scanf("%d", &a[i]), f[i] = 1; res = 0; for (int i = 1; i <= m; i++) { scanf("%d%d", &s, &t); ans = INF; dfs(s, 0); (res += (LL)ans*i%mod) %= mod; } printf("%d\n", res); } return 0;}现在补上floyd的,这题数据不多,用读入优化的意义不大#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int maxn=1e5+10;const int mod=1e9+7;int T,n,m,a[6],dis[6][6],x,y;int main(){scanf("%d",&T);while (T--){scanf("%d%d",&n,&m);for (int i=0;i<6;i++) scanf("%d",&a[i]);for (int i=0;i<6;i++) for (int j=0;j<6;j++) dis[i][j]=abs(a[i]-a[j]);for (int i=0;i<6;i+=2) dis[i][i^1]=dis[i^1][i]=1;for (int i=0;i<6;i++)for (int j=0;j<6;j++)for (int k=0;k<6;k++)dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);int res=0;for (int k=1;k<=m;k++){scanf("%d%d",&x,&y);int ans=abs(x-y);for (int i=0;i<6;i++)for (int j=0;j<6;j++){ans=min(ans,abs(x-a[i])+dis[i][j]+abs(y-a[j]));}(res+=(LL)k*ans%mod)%=mod;}printf("%d\n",res);}return 0;}
0 0
- HDU 5636 Shortest Path
- hdu 5636 Shortest Path
- HDU 5636 Shortest Path
- HDU 5636:Shortest Path floyd
- HDU 5636:Shortest Path【Floyd】
- hdu 5636 Shortest Path【floyd+思维】
- HDU 5636 Shortest Path floyd应用
- HDU 5636 Shortest Path(Floyed,枚举)
- HDU 5636 Shortest Path(最短路)
- Hdu 5636 Shortest Path【最短路+暴力】
- hdu 5636 Shortest Path(Floyd最短路)
- HDU Problem 5636 Shortest Path 【Floyd】
- HDU 5636 Shortest Path(Floyd)
- hdu 3631 Shortest Path
- hdu 3631 Shortest Path
- hdu 3631 Shortest Path
- Hdu 3631 Shortest Path
- HDU 3631 Shortest Path
- PAT-A1089. Insert or Merge (25)
- PL/SQL之六过程与函数
- PAT (Advanced Level) Practise 1030 Travel Plan (30)
- 自是人生长恨水长东
- Android学习笔记----Handler
- HDU 5636 Shortest Path
- PL/SQL编程之七-包
- DisplayMetrics
- 欢迎使用CSDN-markdown编辑器
- Android四大组件详解
- web service design log
- PHP语言代码漏洞审计技巧笔记分享
- 排序算法:选择排序
- 阿里巴巴Dubbo实现的源码分析