人傻了 测试记录
然后我下载了数据
输入:
2 3
1 1 0
1 2 0
2 1 1
输出:
2
然后我一试:
?????
格式错误?求救!
Code(用了dijkstra):
#include<bits/stdc++.h>
//#include<windows.h>
using namespace std;
int Map[1005][1005],dis[1005][1005],change[1005][1005],tot,n,m,dx[5]={0,1,0,-1,0},dy[5]={0,0,1,0,-1};
bool vis[1005][1005],f=false;
double read(){
double sum=0;int f=1,w=10;char c=getchar();bool k=true;
while(!(c>='0'&&c<='9')){if(c=='-')f=-1;c=getchar();}
while((c>='0'&&c<='9')||c=='.'){
if(c=='.'){k=false;c=getchar();continue;}
if(k)sum=sum*10+c-'0';
else sum+=(c-'0')*1.0/w,w*=10;
c=getchar();
}
return sum*f;
}
struct data{
int len,x,y;
bool operator <(const data &a)const{
return len>a.len;
}
};
int distan(int a,int b,int c,int d){
f=false;
if((!Map[a][b]&&!change[a][b])||(!Map[c][d]&&!Map[a][b]))return 1e9+1;
//不能再用或当前点没有色彩
else if(!Map[c][d]&&Map[a][b]){f=true;return 2;}
//实块向虚块
else if(Map[c][d]&&!Map[a][b]){if(change[a][b]==Map[c][d])return 0;return 1;}
//虚块向实块
else {if(Map[a][b]==Map[c][d])return 0;return 1;}
//实块向实块
}
priority_queue<data> DIS;
int main(){
// freopen("chess.in","r",stdin);
// freopen("chess.out","w",stdout);
n=read();m=read();
for(int i=1;i<=m;i++)Map[(int)read()][(int)read()]=read()+1;
//0->1 1->2
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dis[i][j]=1e9;
dis[1][1]=0;DIS.push((data){0,1,1});
while(!DIS.empty()){
int x=DIS.top().x,y=DIS.top().y;
DIS.pop();
if(!vis[x][y])
for(int i=1;i<=4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(distan(x,y,xx,yy)+dis[x][y]<dis[xx][yy]&&xx>=1&&xx<=n&&y>=1&&y<=n){
dis[xx][yy]=distan(x,y,xx,yy)+dis[x][y];
if(f)change[xx][yy]=Map[x][y];
DIS.push((data){dis[xx][yy],xx,yy});
}
}
change[x][y]=0;
//走过后就没有色彩
vis[x][y]=true;
}
if(dis[n][n]>=1e9)cout<<-1;
else cout<<dis[n][n];
return 0;
}