Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+dij
来源:互联网 发布:软件工程质量控制措施 编辑:程序博客网 时间:2024/05/17 06:05
题目链接:http://codeforces.com/contest/821/problem/D
原文地址:点击打开链接
非常好的题,看了题解才会做,建图思路非常巧妙
题意:
有一个n*m的矩形上面有k盏灯,每盏灯占一个格子。只能走有灯的格子,但是当在一个原始就有灯的地方可以花费1使一行或者一列变亮以供行走。现在要求你从左上(1,1)走到右下(n,m),求最小花费。
题解:
这题可以将k个点看做图中的点,由于站在一个初始亮的点上可以将一行或一列变亮,所以我们可以在这个点上,点亮相邻的一行或一列,然后再到相邻行列上的任意点。这样就能够想到了再引入行和列作为图中的点,然后以k个点k行k列作为图的顶点建图。从一个点到相邻行列花费是1,反向花费是0,然后跑一遍最短路就可以了。
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<map>#include<queue>#include<vector>using namespace std;typedef pair<int,int>P;const int maxn=6e5+5;const int inf=0x3f3f3f3f;struct edge{ int to,cost; edge(){}; edge(int to,int cost):to(to),cost(cost){};};vector<edge>G[maxn];int dis[maxn],vis[maxn];P p[maxn];map<P,int> maze;int next[4][2]={0,1,1,0,0,-1,-1,0};void dij(int s){memset(dis,inf,sizeof(dis));priority_queue<P,vector<P>,greater<P> > q;dis[s]=0;q.push(make_pair(dis[s],s));while(!q.empty()){P tmp=q.top();q.pop();int v=tmp.second;if(vis[v]) continue; vis[v]=1;for(int i=0;i<G[v].size();i++) { edge e=G[v][i]; if(dis[e.to]>dis[v]+e.cost) { dis[e.to]=dis[v]+e.cost; q.push(P(dis[e.to],e.to)); } }}}int main(){ int n,m,k; cin>>n>>m>>k; int dd=1e4+5; maze.clear(); for(int i=1;i<=k;i++) { int x,y; scanf("%d%d",&x,&y); G[i].push_back(edge(x+dd,1)); G[i].push_back(edge(y+2*dd,1)); G[i].push_back(edge(x+1+dd,1)); G[i].push_back(edge(y+1+2*dd,1)); G[i].push_back(edge(x-1+dd,1)); G[i].push_back(edge(y-1+2*dd,1)); G[x+dd].push_back(edge(i,0)); G[y+2*dd].push_back(edge(i,0)); G[x+1+dd].push_back(edge(i,0)); G[y+1+2*dd].push_back(edge(i,0)); G[x-1+dd].push_back(edge(i,0)); G[y-1+2*dd].push_back(edge(i,0)); p[i]=P(x,y); maze[P(x,y)]=i; } for(int i=1;i<=k;i++) { for(int j=0;j<4;j++) { int tx=p[i].first+next[j][0]; int ty=p[i].second+next[j][1]; if(maze.find(P(tx,ty))!=maze.end()) { G[i].push_back(edge(maze[P(tx,ty)],0)); } } } if(maze.find(P(n,m))==maze.end()) { G[n+dd].push_back(edge(k+1,0)); G[m+2*dd].push_back(edge(k+1,0)); } dij(maze[P(1,1)]); int ans; if(maze.find(P(n,m))==maze.end()) ans=dis[k+1]; else ans=dis[maze[P(n,m)]]; if(ans==inf) ans=-1; cout<<ans<<endl; return 0;}
阅读全文
0 0
- Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+dij
- Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+spfa
- Codeforces Round #420 (Div. 2) 821D Okabe and City
- CF-Codeforces Round #420 (Div. 2)-D-Okabe and City
- Codeforces 821D Okabe and City【思维建图+Dij+优先队列优化】好题~好题~
- Codeforces Round #420 (Div. 2) D. Okabe and City (最短路)
- Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)
- Codeforces Round #420 (Div. 2) D. Okabe and City 最短路
- Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)
- Codeforces 821D Okabe and City (拆点+思维建图+spfa)
- Codeforces Round #420 (Div. 2) C. Okabe and Boxes 思维
- Codeforces 821 D. Okabe and City
- Codeforces 821D Okabe and City 题解
- codeforces 821d Okabe and City
- Codeforces Round #420 (Div. 2) B. Okabe and Banana Trees
- Codeforces Round #420 (Div. 2) C. Okabe and Boxes
- CF-Codeforces Round #420 (Div. 2)-C-Okabe and Boxes
- Codeforces 821D Okabe And City 最短路
- 自定义view柱状图
- Linux下安装LoadRunner LoadGenerator
- Maven快照机制(SNAPSHOT)
- 生动形象的描述并查集(转)
- Qt QPainter动态进度条[根据值大小改变进度]
- Codeforces Round #420 (Div. 2) 821D Okabe and City 思维建图+dij
- 上海磐启微电子面试经验分享
- Glide加载网络图片报404问题
- ....Fragment must be a public static class to be properly recreated from instance state.
- send和recv函数分析
- IOS中的Block与C++11中的lambda
- Xcode 使用教程(简版)
- 2017夏天之哪凉快去哪里
- springMVC下,MyBatis实现复杂查询