您的位置:首页 > 其它

HDU 5636 关键点的 floyd 最短路问题

2016-03-05 23:36 344 查看

Shortest Path

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 285 Accepted Submission(s): 92


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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 integer n 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 next m lines, each contains two integers si and ti (1≤si,ti≤n), denoting a query.

The sum of values of m in all test cases doesn't exceed 106.

[align=left]Output[/align]
For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7), where zi is the answer for i-th query.

[align=left]Sample Input[/align]

1
10 2
2 4 5 7 8 10
1 5
3 1

[align=left]Sample Output[/align]

7

[align=left]Source[/align]
BestCoder Round #74 (div.2)

bc 题对于我们这种渣渣来说 太感人了
看了题解做的 比赛的时候 传统的最短路都会超时不用想 想dfs 但姿势表达太渣
求最短路 初始时 单链 每点间距离为1 另添加距离为1的3条边 也就是由关键的6个点
关键6个点floyd 处理 具体看代码 理解这个都好久 还是太弱
在询问中 6*6 种与查询区间枚举比较找到 最短路

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define ll __int64
using namespace std;
ll t;
ll n,q;
ll a[10];
ll dis[10][10];
int main()
{
scanf("%I64d",&t);
for(int i=1;i<=t;i++)
{
memset(dis,0,sizeof(dis));
scanf("%I64d %I64d",&n,&q);
scanf("%I64d %I64d %I64d %I64d %I64d %I64d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);
ll sum=0;
for(int j=1;j<=6;j++)
for(int k=1;k<=6;k++)
{
dis[j][k]=abs(a[j]-a[k]);
}
dis[1][2]=1;
dis[2][1]=1;
dis[3][4]=1;
dis[4][3]=1;
dis[5][6]=1;
dis[6][5]=1;
for(int i=1;i<=6;i++)
for(int k=1;k<=6;k++)
for(int m=1;m<=6;m++)
{
if(dis[k][i]+dis[i][m]<dis[k][m])
dis[k][m]=dis[k][i]+dis[i][m];
}
for(int j=1;j<=q;j++)
{
ll aa,bb,t;
scanf("%I64d %I64d",&aa,&bb);
if(aa>bb)
{
t=aa;
aa=bb;
bb=t;
}
ll ans=abs(bb-aa);
for(int k=1;k<=6;k++)
for(int m=1;m<=6;m++)
{
if(ans>abs(aa-a[k])+abs(bb-a[m])+dis[k][m])
ans=abs(aa-a[k])+abs(bb-a[m])+dis[k][m];
}
sum=(sum+j*ans)%1000000007;
}
printf("%I64d\n",sum);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: