bzoj1748 [Usaco2005 open]Around the world 环球飞行
2017-06-07 09:37
495 查看
又见神题= =
这其实是最短路的变形,我们用bfs来跑最短路,然后用map存一下状态,判断有没有重复走。然后最短路的话稍微有一点不一样,从当前点到下一个点有两种路径,如果倒着走就要用现有路径减去,正着走就直接加。
每一次将新的点加入队列答案都加一(显然),当下一个点为1时而且路径不为0(防止你一开始走出去就折返回来),就输出答案。
这其实是最短路的变形,我们用bfs来跑最短路,然后用map存一下状态,判断有没有重复走。然后最短路的话稍微有一点不一样,从当前点到下一个点有两种路径,如果倒着走就要用现有路径减去,正着走就直接加。
每一次将新的点加入队列答案都加一(显然),当下一个点为1时而且路径不为0(防止你一开始走出去就折返回来),就输出答案。
#include<cstdio> #include<algorithm> #include<cstring> #include<map> #include<queue> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std; typedef pair<int,int> P; const int N=5e3+5; const int M=1e5+5; int go[M],next[M],head[M],n,m; int a[M]; map<P,int>t; queue<P> q; int tot; inline void add(int x,int y) { go[++tot]=y; next[tot]=head[x]; head[x]=tot; } int main() { scanf("%d%d",&n,&m); fo(i,1,n)scanf("%d",&a[i]); fo(i,1,m) { int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } q.push(P(1,0)); t[P(1,0)]=1; while (!q.empty()) { P x=q.front(); int y=t[x]; q.pop(); for(int i=head[x.first];i;i=next[i]) { int j=(a[x.first]-a[go[i]]+360)%360; int k=(a[go[i]]-a[x.first]+360)%360; P z(go[i],j<k?x.second+j:x.second-k); if (t[z])continue; if (go[i]==1&&z.second) { printf("%d\n",y); return 0; } q.push(z),t[z]=y+1; } } printf("-1\n"); return 0; }
相关文章推荐
- 【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)
- Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜
- bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 bfs
- bzoj1621【Usaco2008 Open】Roads Around The Farm
- bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通(BFS)
- BZOJ_1621_[Usaco2008_Open]_Roads_Around_The_Farm_分岔路口(模拟+大水题)
- bzoj1687: [Usaco2005 Open]Navigating the City 城市交通
- bzoj:1687;poj 2434:[Usaco2005 Open]Navigating the City 城市交通
- BZOJ1687: [Usaco2005 Open]Navigating the City 城市交通
- poj 2434;bzoj 1686 [Usaco2005 Open]Waves 波纹
- bzoj4099: [Usaco2015 Open]Trapped in the Haybales
- BZOJ1753 [Usaco2005 qua]Who's in the Middle
- 【状压dp】【bitset】bzoj1688 [Usaco2005 Open]Disease Manangement 疾病管理
- BZOJ 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )
- BZOJ1736 [Usaco2005 jan]The Wedding Juicer 婚宴的榨汁机
- bzoj:1665 [Usaco2006 Open]The Climbing Wall 攀岩
- 【BZOJ】1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压dp)
- 树形递归——1621: [Usaco2008 Open]Roads Around The Farm
- bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
- 【BZOJ】1665: [Usaco2006 Open]The Climbing Wall 攀岩(spfa)