Cod sursa(job #1709931)

Utilizator UTCN_TBDUTCN Furdui Moldovan Militaru UTCN_TBD Data 28 mai 2016 14:30:12
Problema Robo Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.33 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <stdlib.h>
using namespace std;

ifstream fin("robo.in");
ofstream fout("robo.out");

vector<int> finalStates;

char buf[100000];
char *a;

vector<int> graph[5205];
char viz[5205];

bool isFinalState(int s) {
	for (int i = 0; i < finalStates.size(); i++) {
		if (s == finalStates[i]) {
			return true;
		}
	}
	return false;
}

int main()
{
	int t, n, m;
	int start, end;
	char act;
	fin >> t;
	for (int tt = 0; tt < t; tt++) {
		fin >> n >> m;
		fin.getline(buf, 100000);
		fin.getline(buf, 100000);
		a = strtok(buf, " ");
		while (a != NULL) {
			finalStates.push_back(atoi(a));
			a = strtok(NULL, buf);
		}
		for (int i = 0; i < n * m; i++) {
			fin >> start >> act >> end;
			graph[start].push_back(end);
			graph[end].push_back(start);
		}
		memset(viz, 0, 5205);
		queue<pair<int, int> > q;
		q.push(make_pair(0, 1));
		viz[0] = 1;
		while (!q.empty()) {
			pair<int, int> curr = q.front();
			for (int j = 0; j < graph[curr.first].size(); j++) {
				int next = graph[curr.first][j];
				if (!viz[next]) {
					viz[next] = 1;
					if (isFinalState(next)) {
						fout << curr.second + 1 << '\n';
					}
					q.push(make_pair(next, curr.second + 1));
				}
			}
			q.pop();
		}
	}
}