Shortest Path
Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1494 Accepted Submission(s): 476
You are given the graph and several queries about the shortest path between some pairs of vertices.
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.
10 2
2 4 5 7 8 10
1 5
3 1
for(int k = 1; k<=6; k++)
for(int i = 1; i<=6; i++)
for(int j = 1; j<=6; j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+;
int d[][];
int a[];
void floyd(){
for(int k = ; k<=; k++)
for(int i = ; i<=; i++)
for(int j = ; j<=; j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
void solve(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = ; i<=; i++) scanf("%d",&a[i]);
for(int i = ; i<=; i++){
for(int j = ; j<=; j++){
d[i][j] = abs(a[i] - a[j]);
}
}
for(int i = ; i<=; i+=) d[i][i+] = d[i+][i] = ;
floyd();
long long sum = ; for(int k = ; k<=m; k++){
int x,y;
scanf("%d%d",&x,&y);
int ans = abs(x-y);
for(int i = ; i<=; i++)
for(int j = ; j<=; j++)
ans = min(ans,(abs(x-a[i])+d[i][j]+abs(y-a[j])));
sum = (sum + (long long)ans*k%mod)%mod;
}
printf("%I64d\n",sum);
}
}
int main()
{
solve();
return ;
}
卷珠帘
Shortest Path的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- OSPF(Open Shortest Path First)
1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的,所以所有厂商都可以用. OSPF的流量使用IP协议号. O ...
随机推荐
- LeetCode OJ 96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Python之路:爬虫之urllib库的基本使用和高级使用
关于爬虫自己一直在看,所以时间太慢,这才第二更,有等不及的小伙伴可以慢慢的品尝了,在看下面的之前,建议先把上一章看一下.以下是关于python的Urllib的基础和高级用法. 1.如何扒下一个网站,用 ...
- android动态添加TextView或者ImageView
动态添加 text1=new TextView(this); text1.setText("动态添加"); ((LinearLayout) this.findViewById(R. ...
- SpringSecurity自定义过滤器
applicationContext-security.xml: <beans:beans xmlns="http://www.springframework.org/schema/s ...
- 关于video.js
网址:http://www.cnblogs.com/webenh/p/5815741.html
- asp.net html table to DataTable
添加引用 http://htmlagilitypack.codeplex.com/downloads/get/437941 protected void Export(string content,s ...
- Inno Setup入门(十六)——Inno Setup类参考(2)
分类: Install Setup 2013-02-02 11:28 815人阅读 评论(0) 收藏 举报 这里将接着在前面的基础上介绍如何在自定义页面上添加按钮.按钮属于Tbutton类,该类继承自 ...
- mongodb状态
基本信息 spock:PRIMARY>db.serverStatus() { "host" :"h6.corp.yongche.org", //主机名 & ...
- zookeeper集群实例
zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...
- RS232 DB9 公头 母头 串口引脚定义
转自:http://hi.baidu.com/zhy_myspace/item/ad1ab0f112a4da1ad7ff8cf9 DB9 公头 母头 串口引脚定义 1.RS-232端(DB9母头/孔型 ...