POJ 5636 Shortest Path(floyd)
来源:互联网 发布:p2p数据分析报告 编辑:程序博客网 时间:2024/05/16 10:32
Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1072 Accepted Submission(s): 340
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个节点的图,任意节点i和i+1之间有权值为1的边,现在往里面加入三条边权值为1的边,求S=(∑i=1mi⋅zi),zi为第i对点之间的最短距离。
解题思路:直接对所给的6个点求任意两点的最短路,然后计算所给点对通过这些点的最短路即可。
代码如下:
/*time:author:tpblueskyanswer:*/#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <vector>#include <set>#include <list>#include <map>#include <stack>#include <queue>#include <cmath>#include <cstdlib>#include <sstream>#define inf 0x3f3f3f3f#define eps 1e-6#define sqr(x) ((x)*(x))using namespace std;typedef long long ll;const int maxn = 10;const int mod = 1000000007;int a[maxn], b[maxn], n, m;int mp[maxn][maxn];int getpos(int x){return lower_bound(a,a+6,x)-a;}int main(){int T;cin>>T;while(T--){scanf("%d%d",&n,&m);for(int i = 0;i < 6;++i){scanf("%d",&a[i]);b[i] = a[i];}sort(a,a+6);memset(mp,0,sizeof(mp));for(int i = 0;i < 6;++i)for(int j = 0;j < 6;++j)mp[i][j] = abs(a[i]-a[j]);for(int i = 0;i < 6;i+= 2){int x = getpos(b[i]);int y = getpos(b[i+1]);mp[x][y] = mp[y][x] = min(mp[x][y],1);} //floydfor(int k = 0;k < 6;++k)for(int i = 0;i < 6;++i)for(int j = 0;j < 6;++j)mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);ll ans = 0;int x, y;for(int k = 1;k <= m;++k){scanf("%d%d",&x,&y);ll res = abs(x-y);for(int i = 0;i < 6;++i)for(int j = 0;j < 6;++j)res = min(res,(ll)(abs(x-a[i])+abs(y-a[j])+mp[i][j]));ans = ((ans+(res*k)%mod)%mod);}printf("%lld\n",ans);} return 0;}
0 0
- POJ 5636 Shortest Path(floyd)
- POJ 3631 SHORTEST PATH(FLOYD改装)
- HDU 5636 Shortest Path(Floyd)
- HDU 5636:Shortest Path floyd
- HDU 5636:Shortest Path【Floyd】
- HDU-5636(Shortest Path)(floyd最短路径)
- 杭电5636 Shortest Path (Floyd最短路)
- HDU:5636 Shortest Path(floyd+最短路径)
- hdu 5636 Shortest Path【floyd+思维】
- HDU 5636 Shortest Path floyd应用
- hdu 5636 Shortest Path(Floyd最短路)
- HDU Problem 5636 Shortest Path 【Floyd】
- hdu 3631 Shortest Path(floyd插点法)
- hdu 3631 Shortest Path(Floyd)
- hdu3631 Shortest Path (floyd 变形)
- HDU2807The Shortest Path(SPFA或Floyd)
- hdu 3631 Shortest Path(Floyd)
- hdu 3631 Shortest Path (floyd)
- 把Android项目迁移到Android Studio常见的错误
- codeforces 650A Watchmen 【数学】
- Visual Studio 路径宏
- 部署在WildFly上的EJB客户端,调用另一个WildFly上的EJB服务的过程详解
- JS数字转大写中文金钱JS函数
- POJ 5636 Shortest Path(floyd)
- (无法推断有效的主键。已排除该表/视图)Entity Framework 无法对没有主键的视图映射实体的解决办法
- 【FFMPEG】FFMPEG程序捕获Mac设备流媒体
- mysql导出数据表结构,必须退出mysql命令.重新使用msyqldump命令
- mac 上找不到 lippicv, 坑
- Swift的一些基本属性1 (常量变量)
- css实现h5页面滚动效果
- @PostConstruct和@PreDestroy
- 解决Please choose a writable location using the '-configuration' command line option"