Submission #19664


Source Code Expand

Copy
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>

#define REP(i,x) for(int i=0 ; i<(int)(x) ; i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long

using namespace std;

class C{
	struct Node{
		int next;
		int sec;
		double cost;
		Node(int n,int s):next(n),sec(s){}
		Node(int n,int s,double c):next(n),sec(s),cost(c){}
	};
	int N,M;
	int sy,sx,gy,gx;
	vector<string> field;
	vector<double> pre;
	bool isValid(int y,int x){
		return y>=0 && y<N && x>=0 && x<M;
	}
public:
	bool init(){
		if(scanf("%d%d",&N,&M)==EOF)return false;
		field.resize(N);
		REP(i,N){
			char buf[1024];
			scanf("%s",buf);
			field[i] = string(buf);
		}
		return true;
	}
	double check(double mid,bool isFirst=true){
		vector<vector<double> > used(N,vector<double>(M,-2));
		queue<Node> que;que.push(Node(sy*M+sx,0,100));
		while(!que.empty()){
			Node n = que.front();que.pop();
			int cy = n.next/M;
			int cx = n.next%M;
			if(cy==gy && cx==gx)return used[cy][cx];
			const int DY[] = {-1,0,1,0};
			const int DX[] = {0,1,0,-1};
			REP(i,4){
				int ny = cy+DY[i];
				int nx = cx+DX[i];
				
				if(!isValid(ny,nx) || field[ny][nx]=='#')continue;
				double cost = field[ny][nx]-'0';
				cost *= pre[n.sec+1];
				if(field[ny][nx]=='g')cost = 100;
				cost = min(n.cost,cost);
				if(cost>mid && used[ny][nx]<cost){
					if((isFirst && used[ny][nx]<0) || !isFirst){
						used[ny][nx] = cost;
						que.push(Node(ny*M+nx,n.sec+1,cost));
					}
				}
			}
		}
		return -1;
	}
	void solve(){
		REP(y,N)REP(x,M){
			if(field[y][x]=='s'){
				sy = y;
				sx = x;
			}
			if(field[y][x]=='g'){
				gy = y;
				gx = x;
			}
		}
		pre.assign(4*N*M,1);
		REP(i,4*N*M-1)pre[i+1] = pre[i]*0.99;
		vector<vector<double> > used(N,vector<double>(M,-2));
		vector<vector<int> > inQue(N,vector<int>(M,0));
		queue<Node> que;que.push(Node(sy*M+sx,0));
		inQue[sy][sx] = 1;
		used[sy][sx] = 100;
		while(!que.empty()){
			Node n = que.front();que.pop();
			int cy = n.next/M;
			int cx = n.next%M;
			//cout << cy << "," << cx << " " << used[cy][cx] << endl;
			inQue[cy][cx] = 0;
			const int DY[] = {-1,0,1,0};
			const int DX[] = {0,1,0,-1};
			REP(i,4){
				int ny = cy+DY[i];
				int nx = cx+DX[i];
				
				if(!isValid(ny,nx) || field[ny][nx]=='#')continue;
				double cost = field[ny][nx]-'0';
				cost *= pre[n.sec+1];
				if(field[ny][nx]=='g')cost = 100;
				cost = min(used[cy][cx],cost);
				if(used[ny][nx]<cost){
					used[ny][nx] = cost;
					if(!inQue[ny][nx]){
						inQue[ny][nx] = 1;
						que.push(Node(ny*M+nx,n.sec+1));
					}
				}
			}
		}
		double lb = check(0,true),ub = 10;
		if(lb>0){
			REP(i,50){
				double mid = (lb+ub)/2;
				if(check(mid)>0)lb = mid;
				else ub = mid;
			}
			printf("%.9f\n",lb);
		}
		else printf("-1\n");
		//if(used[gy][gx]<-1)printf("-1\n");
		//else printf("%.9f\n",(double)used[gy][gx]);
	}
};

int main(){
	C solver;
	while(solver.init()){
		solver.solve();
	}
	return 0;
}

Submission Info

Submission Time
Task C - 暗闇帰り道
User nel215
Language C++ (G++ 4.6.4)
Score 100
Code Size 3288 Byte
Status AC
Exec Time 1431 ms
Memory 13836 KB

Compile Error

./Main.cpp: In member function ‘bool C::init()’:
./Main.cpp:45:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]

Judge Result

Set Name all
Score / Max Score 100 / 100
Status
AC × 64
Set Name Test Cases
all 00_mini_01.txt, 00_mini_02.txt, 00_mini_03.txt, 00_mini_04.txt, 00_mini_05.txt, 00_sample_01.txt, 00_sample_02.txt, 01_rnd_01.txt, 01_rnd_02.txt, 01_rnd_03.txt, 01_rnd_04.txt, 01_rnd_05.txt, 01_rnd_06.txt, 01_rnd_07.txt, 01_rnd_08.txt, 01_rnd_09.txt, 01_rnd_10.txt, 01_rnd_11.txt, 01_rnd_12.txt, 01_rnd_13.txt, 01_rnd_14.txt, 01_rnd_15.txt, 01_rnd_16.txt, 01_rnd_17.txt, 01_rnd_18.txt, 01_rnd_19.txt, 01_rnd_20.txt, 02_maxrnd_01.txt, 02_maxrnd_02.txt, 02_maxrnd_03.txt, 02_maxrnd_04.txt, 02_maxrnd_05.txt, 02_maxrnd_06.txt, 02_maxrnd_07.txt, 02_maxrnd_08.txt, 02_maxrnd_09.txt, 02_maxrnd_10.txt, 02_maxrnd_11.txt, 02_maxrnd_12.txt, 02_maxrnd_13.txt, 02_maxrnd_14.txt, 02_maxrnd_15.txt, 02_maxrnd_16.txt, 02_maxrnd_17.txt, 02_maxrnd_18.txt, 02_maxrnd_19.txt, 03_hard_01.txt, 03_hard_02.txt, 03_hard_03.txt, 03_hard_04.txt, 03_hard_05.txt, 03_hard_06.txt, 03_hard_07.txt, 03_hard_08.txt, 04_open_01.txt, 04_open_02.txt, 05_minihard_01.txt, 05_minihard_02.txt, 05_minihard_03.txt, 05_minihard_04.txt, 05_minihard_05.txt, 05_minihard_06.txt, 05_minihard_07.txt, 05_minihard_08.txt
Case Name Status Exec Time Memory
00_mini_01.txt AC 21 ms 812 KB
00_mini_02.txt AC 21 ms 788 KB
00_mini_03.txt AC 21 ms 788 KB
00_mini_04.txt AC 22 ms 760 KB
00_mini_05.txt AC 21 ms 796 KB
00_sample_01.txt AC 21 ms 788 KB
00_sample_02.txt AC 21 ms 796 KB
01_rnd_01.txt AC 1228 ms 10888 KB
01_rnd_02.txt AC 254 ms 5448 KB
01_rnd_03.txt AC 54 ms 2072 KB
01_rnd_04.txt AC 127 ms 2568 KB
01_rnd_05.txt AC 35 ms 1280 KB
01_rnd_06.txt AC 25 ms 916 KB
01_rnd_07.txt AC 54 ms 2052 KB
01_rnd_08.txt AC 347 ms 6752 KB
01_rnd_09.txt AC 518 ms 9512 KB
01_rnd_10.txt AC 595 ms 8432 KB
01_rnd_11.txt AC 339 ms 5988 KB
01_rnd_12.txt AC 217 ms 4304 KB
01_rnd_13.txt AC 52 ms 1428 KB
01_rnd_14.txt AC 706 ms 8264 KB
01_rnd_15.txt AC 335 ms 5672 KB
01_rnd_16.txt AC 74 ms 3836 KB
01_rnd_17.txt AC 66 ms 5376 KB
01_rnd_18.txt AC 22 ms 1432 KB
01_rnd_19.txt AC 34 ms 3072 KB
01_rnd_20.txt AC 21 ms 1436 KB
02_maxrnd_01.txt AC 583 ms 13824 KB
02_maxrnd_02.txt AC 514 ms 13828 KB
02_maxrnd_03.txt AC 638 ms 13824 KB
02_maxrnd_04.txt AC 910 ms 13820 KB
02_maxrnd_05.txt AC 642 ms 13820 KB
02_maxrnd_06.txt AC 1421 ms 13820 KB
02_maxrnd_07.txt AC 430 ms 13824 KB
02_maxrnd_08.txt AC 389 ms 13836 KB
02_maxrnd_09.txt AC 1431 ms 13836 KB
02_maxrnd_10.txt AC 502 ms 13820 KB
02_maxrnd_11.txt AC 1328 ms 13836 KB
02_maxrnd_12.txt AC 779 ms 13824 KB
02_maxrnd_13.txt AC 936 ms 13824 KB
02_maxrnd_14.txt AC 1026 ms 13832 KB
02_maxrnd_15.txt AC 398 ms 13832 KB
02_maxrnd_16.txt AC 805 ms 13828 KB
02_maxrnd_17.txt AC 655 ms 13820 KB
02_maxrnd_18.txt AC 123 ms 13780 KB
02_maxrnd_19.txt AC 123 ms 13816 KB
03_hard_01.txt AC 1171 ms 13828 KB
03_hard_02.txt AC 1245 ms 13820 KB
03_hard_03.txt AC 333 ms 13828 KB
03_hard_04.txt AC 337 ms 13824 KB
03_hard_05.txt AC 1185 ms 13824 KB
03_hard_06.txt AC 1242 ms 13824 KB
03_hard_07.txt AC 340 ms 13816 KB
03_hard_08.txt AC 339 ms 13824 KB
04_open_01.txt AC 1326 ms 13820 KB
04_open_02.txt AC 1399 ms 13828 KB
05_minihard_01.txt AC 23 ms 732 KB
05_minihard_02.txt AC 23 ms 792 KB
05_minihard_03.txt AC 23 ms 736 KB
05_minihard_04.txt AC 22 ms 792 KB
05_minihard_05.txt AC 24 ms 796 KB
05_minihard_06.txt AC 24 ms 796 KB
05_minihard_07.txt AC 21 ms 788 KB
05_minihard_08.txt AC 23 ms 780 KB