算法:最短路(卡for循环(不卡memset)...)
难度:NOIP
为什么memset清数组没有for循环快??????
题目不难,但是为什么luogu一个点只给150ms???,人家bzoj所有点给了10s!(luogu不开O2的话,真心过不去) 回到正题,跑最短路,如果需要改变边的方向,边权就是1,否则就是0,结束了。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define N 705
#define M 600005
using namespace std;
int mapp[N][N];
int cnt=1,cot=1;
struct node
{
int next;
int to;
int val;
}edge[M<<1];
int head[M<<1],dis[N*N];
bool vis[N*N];
void init()
{
/*for(int i = 1;i <= M<<1-3;i++)
{
head[i]=-1;
}*/
memset(head,-1,sizeof(head));
//memset(edge,0,sizeof(edge));
memset(dis,0x3f3f3f3f,sizeof(dis));
/*for(int i = 1;i <= N*N-2;i++)
{
dis[i]=0x3f3f3f3f;
}*/
cnt=1;
}
void read(int &x)
{
int f=1;x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
void add(int u,int v,int w)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
edge[cnt].val=w;
head[u]=cnt++;
}
struct no
{
int d;
int po;
};
bool operator < (no a,no b)
{
return a.d > b.d;
}
void dijkstra(int rt)
{
priority_queue<no>Q;
no point;
point.d=0;
point.po=rt;
Q.push(point);
dis[rt]=0;
while(!Q.empty())
{
point=Q.top();
int u=point.po;
Q.pop();
if(vis[u]) continue;
vis[u]=1;
for(int i = head[u];i != -1;i = edge[i].next)
{
int to = edge[i].to;
if(vis[to])
{
continue;
}
if(dis[to] > edge[i].val + dis[u])
{
dis[to] = edge[i].val + dis[u];
no temp;
temp.d = dis[to];
temp.po = to;
Q.push(temp);
}
}
}
}
int main()
{
int T;
//scanf("%d",&T);
for(int i = 0;i <= 504;i++)
{
for(int j = 0;j <= 504;j++)
{
mapp[i][j]=cot++;
}
}
char str[505];//每次直接读入一行,数组又开小了
//while(T--)
//{
init();
int r,c;
//scanf("%d%d",&r,&c);
read(r),read(c);
for(int i = 1;i <= r;i++)
{
//scanf("%s",str+1);
gets(str+1);
for(int j = 1;j <= c;j++)
{
if(str[j]=='/')
{
add(mapp[i-1][j-1],mapp[i][j],1);
add(mapp[i][j],mapp[i-1][j-1],1);
add(mapp[i-1][j],mapp[i][j-1],0);
add(mapp[i][j-1],mapp[i-1][j],0);
}else
{
add(mapp[i-1][j-1],mapp[i][j],0);
add(mapp[i][j],mapp[i-1][j-1],0);
add(mapp[i-1][j],mapp[i][j-1],1);
add(mapp[i][j-1],mapp[i-1][j],1);
}
}
}
dijkstra(mapp[0][0]);
if(dis[mapp[r][c]]==0x3f3f3f3f)
{
puts("NO SOLUTION");
return 0 ;
}
printf("%d\n",dis[mapp[r][c]]);
//}
return 0 ;
}