Shortest Path

 Accepts: 40
 Submissions: 610
 Time Limit: 4000/2000 MS (Java/Others)
 Memory Limit: 131072/131072 K (Java/Others)
Problem Description

There is a path graph G=(V,E)G=(V,E) with nn vertices. Vertices are numbered from 11 to nn and there is an edge with unit length between iiand i + 1i+1 (1 \le i < n)(1≤i<n). To make the graph more interesting, someone adds three more edges to the graph. The length of each new edge is 11.

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 TT, indicating the number of test cases. For each test case:

The first line contains two integer nn and mm (1 \le n, m \le 10^5)(1≤n,m≤10​5​​) -- the number of vertices and the number of queries. The next line contains 6 integers a_1, b_1, a_2, b_2, a_3, b_3a​1​​,b​1​​,a​2​​,b​2​​,a​3​​,b​3​​ (1 \le a_1,a_2,a_3,b_1,b_2,b_3 \le n)(1≤a​1​​,a​2​​,a​3​​,b​1​​,b​2​​,b​3​​≤n), separated by a space, denoting the new added three edges are (a_1,b_1)(a​1​​,b​1​​), (a_2,b_2)(a​2​​,b​2​​), (a_3,b_3)(a​3​​,b​3​​).

In the next mm lines, each contains two integers s_is​i​​ and t_it​i​​ (1 \le s_i, t_i \le n)(1≤s​i​​,t​i​​≤n), denoting a query.

The sum of values of mm in all test cases doesn't exceed 10^610​6​​.

Output

For each test cases, output an integer S=(\displaystyle\sum_{i=1}^{m} i \cdot z_i) \text{ mod } (10^9 + 7)S=(​i=1​∑​m​​i⋅z​i​​) mod (10​9​​+7), where z_iz​i​​ is the answer for ii-th query.

Sample Input
1
10 2
2 4 5 7 8 10
1 5
3 1
Sample Output
7
题解:最短路,本来完全暴力的最短路,果断超时,最后听了大神的想法,是求加的那三条边的最短路;让每次给的u,v找u到那三个起点的距离与
d[v]的和与v-u找最小值;还是超时,先放着吧,明天再看;
今天看了下发现自己写的很有问题,哪有那么麻烦,直接dfs下就好了,其实就是个思维题,用图论肯定超了。。。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+;
const int MOD=1e9+;
typedef long long LL;
struct Dot{
int x,y;
};
Dot dot[];
int u,v;
LL ans;
int vis[];
void dfs(int pos,int temp){
if(temp+abs(v-pos)<ans)ans=temp+abs(v-pos);
for(int i=;i<;i++){
if(vis[i])continue;
vis[i]=;
dfs(dot[i].y,temp+abs(pos-dot[i].x)+);
dfs(dot[i].x,temp+abs(pos-dot[i].y)+);
vis[i]=;
}
}
int main(){
int T;
SI(T);
int N,M;
while(T--){
SI(N);SI(M);
for(int i=;i<;i++)SI(dot[i].x),SI(dot[i].y);
LL temp=;
vis[]=vis[]=vis[]=;
for(int i=;i<=M;i++){
SI(u);SI(v);
ans=abs(v-u);
dfs(u,);
temp=(temp+ans*i)%MOD;
}
printf("%lld\n",temp);
}
return ;
}
我的超时代码:先不说超时了,这样情况都没考虑完全,都没考虑过好几条路的情况;再者这样肯定会超时了。。。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+;
const int MOD=1e9+;
int head[MAXN<<];
int vis[MAXN];
int n,m;
int d1[MAXN],d2[MAXN],d3[MAXN];
int d[MAXN];
struct Node{
int from,to,next;
};
Node dt[MAXN<<];
int edgnum;
void add(int u,int v){
dt[edgnum].from=u;dt[edgnum].to=v;
dt[edgnum].next=head[u];
head[u]=edgnum++;
}
void dijkscra(int u){
mem(vis,);
mem(d,INF);
d[u]=;
while(true){
int k=-;
for(int i=;i<=n;i++){
if(!vis[i])if(k==-||d[i]<d[k])k=i;
}
if(k==-)break;
vis[k]=;
for(int i=head[k];i!=-;i=dt[i].next){
int u=dt[i].from,v=dt[i].to;
//printf("%d %d\n",u,v);
d[v]=min(d[v],d[u]+);
}
}
} /*
int temp;
void dfs(int u,int v,int t){
if(t>n)return;
if(u==v){
temp=min(temp,t);
return;
}
for(int i=head[u];i!=-1;i=dt[i].next){
dfs(dt[i].to,v,t+1);
}
}
*/
int main(){
int T;
SI(T);
while(T--){
SI(n);SI(m);
int a1,a2,a3,b1,b2,b3;
edgnum=;mem(head,-);
for(int i=;i<n;i++)add(i,i+),add(i+,i);
scanf("%d%d%d%d%d%d",&a1,&b1,&a2,&b2,&a3,&b3);
add(a1,b1);add(b1,a1);add(a2,b2);add(b2,a2);add(a3,b3);add(b3,a3);
__int64 ans=;
dijkscra(a1);
for(int i=;i<=n;i++)d1[i]=d[i];
dijkscra(a2);
for(int i=;i<=n;i++)d2[i]=d[i];
dijkscra(a3);
for(int i=;i<=n;i++)d3[i]=d[i]; for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
/*dijkscra(u);*/
int temp=min(min(abs(v-u),abs(u-a1)+d1[v]),min(abs(u-a2)+d2[v],abs(u-a3)+d3[v]));
//printf("%d\n",temp);
ans=(ans+temp*i)%MOD;
}
printf("%I64d\n",ans);
}
return ;
}

Shortest Path(思维,dfs)的更多相关文章

  1. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  2. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  3. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  4. ZOJ 2760 How Many Shortest Path(最短路径+最大流)

    Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...

  5. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  6. zoj How Many Shortest Path

    How Many Shortest Path 题目: 给出一张图,求解最短路有几条.处理特别BT.还有就是要特别处理map[i][i] = 0,数据有不等于0的情况! 竟然脑残到了些错floyd! ! ...

  7. Codefroces Educational Round 27 845G Shortest Path Problem?

    Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...

  8. CF938G Shortest Path Queries 和 CF576E Painting Edges

    这两道都用到了线段树分治和按秩合并可撤销并查集. Shortest Path Queries 给出一个连通带权无向图,边有边权,要求支持 q 个操作: x y d 在原图中加入一条 x 到 y 权值为 ...

  9. leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]

    题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can m ...

随机推荐

  1. android--graphics

    Color类 Constants |____BLACK, BLUE, CYAN Methods |____argb,rgb,alpha, red, green, blue |____parseColo ...

  2. HDU 4082 Hou Yi's secret(暴力)

    直接6重循环就行了吧...判三角形相似直接从小到大枚举两向量夹角是否相等就行了.注意去重点跟三点共线就行了... #include<algorithm> #include<iostr ...

  3. Unix/Linux环境C编程入门教程(32) 环境变量那些事儿

    1. getenv() putenv()setenv()函数介绍 getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdli ...

  4. C++的四种cast操作符的区别--类型转换

    Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而 ...

  5. GetModuleHandle,AfxGetInstanceHandle使用区别

    当一个文件被映射到调用进程的地址空间时,GetModuleHandle函数得到其中某一模块的句柄. 使用GetModuleHandle函数格式:HMODULE GetModuleHandle( LPC ...

  6. wikioi1688 求逆序对

    题目描述 Description 给定一个序列a1,a2,…,an,如果存在i<j并且ai>aj,那么我们称之为逆序对,求逆序对的数目 数据范围:N<=105.Ai<=105. ...

  7. PHP出现Notice: unserialize() [function.unserialize]: Error at offset问题的解决方案

    有两个原因(据我所知)会导致这个问题: (1) 字符串本身的问题 (2)字符编码的问题. 你unserialize的字符串的编码和文件本身的编码不一致.将文件编码改成和字符串一样的编码.这种问题比较隐 ...

  8. Swift自定义Class实现Hashable

    假如有个Bit类,其中含有CGPoint类型的point属性,Class定义如下 class Bit { var point : CGPoint init(point : CGPoint) { sel ...

  9. uva11029 - Leading and Trailing

    题目: 求n的k次方,然后将答案用前三位和最后三位表示. Sample Input 2 123456 1 123456 2 Sample Output 123...456 152...936 分析: ...

  10. oracle 定义数据完整性

    1. 定义主键约束 1.1 在创建表时定义主键约束 create table student(name varchar2(8),studentid varchar2(10) primary key,s ...