BZOJ 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road

题意很简单,不说了。

这题思路其实挺简单,把每个格编号为i,3*i走到3*j+1,3*i+1走到3*j+2,以此类推,注意一下边权是多少就好了.

然而一开始用分层图的普遍套路i+j*n*n编号调了好久发现不同层编号有冲突,mdzz....

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <queue>
  7 #include <map>
  8 #define ll long long
  9 #define out(a) printf("%d ",a)
 10 #define ln printf("\n")
 11 #define clear(a,b) memset(a,b,sizeof(a))
 12 const int N=1e5+2e4+50;
 13 const int MOD=1e9+7;
 14 using namespace std;
 15 int n,nf,t;
 16 int x_,y_,l,r,cnt=0;
 17 int x[150][150],a[150][150];
 18 int tot=0;
 19 int head[N],dis[N];
 20 bool vis[N];
 21 int ans=23333333;
 22 int dx[4]={1,-1,0,0};
 23 int dy[4]={0,0,1,-1};
 24 struct node
 25 {
 26     int to,nxt,cost;
 27 }edge[N<<1];
 28 struct dist
 29 {
 30     int h,id;
 31     bool operator<(const dist&a)const{
 32       return a.h<h;
 33     }
 34 };
 35 int read()
 36 {
 37     int s=0,t=1; char c;
 38     while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
 39     while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
 40     return s*t;
 41 }
 42 ll readl()
 43 {
 44     ll s=0,t=1; char c;
 45     while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
 46     while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
 47     return s*t;
 48 }
 49 void add(int x,int y,int z)
 50 {
 51     edge[++tot].to=y;
 52     edge[tot].cost=z;
 53     edge[tot].nxt=head[x];
 54     head[x]=tot;
 55 }
 56 priority_queue<dist>q;
 57 void Dijkstra(int s)
 58 {
 59     int x,y,z;
 60     clear(dis,127); clear(vis,false);
 61     dis[s]=0;
 62     dist p;
 63     p.h=0; p.id=s;
 64     q.push(p);
 65     while (!q.empty()){
 66       p=q.top(); q.pop();
 67       x=p.id;
 68       if (!vis[x]) {
 69           vis[x]=true;
 70           for (int i=head[x];i;i=edge[i].nxt){
 71             y=edge[i].to; z=edge[i].cost;
 72             if (dis[y]>dis[x]+z){
 73                 dis[y]=dis[x]+z;
 74                 p.h=dis[y]; p.id=y;
 75                 q.push(p);
 76             }
 77           }
 78       }
 79     }
 80     ans=min(min(dis[3*nf],dis[3*nf+1]),dis[3*nf+2]);
 81 }
 82 int main()
 83 {
 84     n=read(),t=read();
 85     for (int i=1;i<=n;i++)
 86       for (int j=1;j<=n;j++)
 87         x[i][j]=read(),a[i][j]=++cnt;
 88     nf=n*n;
 89     for (int i=1;i<=n;i++)
 90       for (int j=1;j<=n;j++){
 91           for (int k=0;k<4;k++){
 92             x_=i+dx[k]; y_=j+dy[k];
 93             if (x_>0&&x_<=n&&y_>0&&y_<=n) {
 94                 l=a[i][j]; r=a[x_][y_];
 95                 add(3*l,3*r+1,t); //add(3*r+1,3*l,t);
 96                 add(3*l+1,3*r+2,t); //add(3*r+2,3*l+1,t);
 97                 add(3*l+2,3*r,x[x_][y_]+t); //add(3*r,3*l+2,x[i][j]+t);
 98             }
 99          }
100      }
101      Dijkstra(3);
102     out(ans);
103     return 0;
104 }
105         
106             
107       
View Code

 

posted @ 2018-09-01 16:21  Kaleidoscope233  阅读(207)  评论(0编辑  收藏  举报