Skip to content

Instantly share code, notes, and snippets.

@a1ien
Created November 18, 2012 14:05
Show Gist options
  • Save a1ien/4105460 to your computer and use it in GitHub Desktop.
Save a1ien/4105460 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdint.h>
#include <vector>
using namespace std;
uint32_t* graph;
uint32_t V = 0;
uint32_t V_c=0;
bool *visited;
void dfs(uint32_t v,uint32_t tank)
{
visited[ v ] = true;
V_c++;
for (uint32_t i = 0;i <= V;i++ )
if ( graph[V*v+i] <=tank && visited[ i ] == false )
dfs(i ,tank);
}
bool check(uint32_t tank)
{
for (uint32_t j = 0;j <= V;++j )
visited[ j ] = false;
V_c=0;
dfs(0,tank);
return V_c==V+1;
}
int main()
{
uint32_t min = 0,max = 0,middle=0;
cin >> V;
graph=new uint32_t[V*V];
visited=new bool[V];
for (uint32_t i = 0; i != V; ++i) {
for (uint32_t j = 0, val=0; j != V; ++j) {
cin >> val;
if(max < val) max = val;
if ((val < min && val) || !min ) min = val;
graph[V*i+j] = val;
}
}
middle = (min+max)/2;
while( min != max )
{
bool res=check(middle);
if (!res )
min = middle + 1;
else
max = middle - 1;
middle = (min + max)/2;
}
cout<<middle<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment