HDU 5636 Shortest Path floyd应用

Shortest Path

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


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   (1i<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.
 

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 integer  n  and  m   (1n,m105)  -- the number of vertices and the number of queries. The next line contains 6 integers  a1,b1,a2,b2,a3,b3   (1a1,a2,a3,b1,b2,b3n) , 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   (1si,tin) , denoting a query.

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

Output
For each test cases, output an integer  S=(i=1mizi) mod (109+7) , where  zi  is the answer for  i -th query.
 

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

Sample Output
  
  
7
 

Source
 



题意:有一条长度为n的链. 节点i和i+1之间有长度为1的边. 现在又新加了3条边, 每条边长度都是1. 给出m个询问, 每次询问两点之间的最短路.


开始的时候想的是把开始六个点预处理了,再把询问的两个点加入跑Floyed,但是这样复杂度太高,会tle。


因为前六个点之间的边权关系永远不会变,所以先对前六个个点跑一遍Floyed,然后输入一个询问处理一个询问,询问u->v的最短路,遍历所有可能的情况,即u->i->j->v,还有u直线到v的距离,一共37种情况,取最小就行了,所以总的复杂度只有O(6^2 * m)。


CODE

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const long long mod = 1e9+7;
const long long INF = 777777777;

LL a[20];        ///记录6个点
LL Map[20][20];  ///记录地图
LL ans;          ///答案

void INIT()      ///初始化
{
    ans = 0;
    for(LL i = 0;i < 10;i++)
    {
        for(LL j = 0;j < 10;j++)
        {
            Map[i][j] = INF;
        }
    }
}

int main()
{
    LL T;
    scanf("%I64d",&T);
    while(T--)
    {
        INIT();
        LL n,m;
        scanf("%I64d%I64d",&n,&m);
        for(LL i = 1; i <= 6; i++)      ///输入6个点
        {
            scanf("%I64d",&a[i]);
            for(LL j = i-1;j >= 1;j--)  ///两个点之间的直线距离
            {
                LL t1 = a[i],t2 = a[j];
                Map[i][j] = Map[j][i] = abs(t1-t2);
            }
        }
        for(LL i = 1; i <= 6; i++)      ///第12   34   56点之间距离为1
        {
            if(i%2 == 0)
            {
                Map[i][i-1] = Map[i-1][i] = 1;
            }
        }
        for(LL i = 1;i <= 6;i++)        ///Floyed
            for(LL j = 1;j <= 6;j++)
                for(LL k = 1;k <= 6;k++)
                    Map[j][k] = min(Map[j][k],Map[j][i]+Map[i][k]);
        for(LL i = 1;i <= m;i++)
        {
            LL u,v;
            scanf("%I64d %I64d",&u,&v);
            LL t = abs(u-v);
            for(int k = 1; k <= 6 ;k++) ///u->i->j->v所有36种可能跑一遍取最小
                for(int j = 1; j <= 6; j++)
                {
                    LL tmp = abs(u-a[k])+Map[k][j]+abs(a[j]-v);
                    t = min(t,tmp);
                }
            ans = (ans%mod+i*t%mod)%mod;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值