UVa 12376 - As Long as I Learn, I Live
/* Accepted */
/*
Algorithm: BFS
Just take the maximum adjacent nodes and calculate the units of it as well as keep
track about the last node where you are terminating your graph traverse.
*/
#include<bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define psb push_back
#define fast ios_base::sync_with_stdio(false)
const int high = 205;
int units[high], visited[high], total = 0;
int mxx = -1 , mxxnode = 0, lastnode = 0;
vector<int>adj[high];
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clear();
units[i] = 0;
visited[i] = 0;
}
total = 0;
}
void BFS(int s)
{
visited[s] = 1;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
lastnode = u;
total += units[u];
Q.pop();
int sze = adj[u].size(), v;
mxx = -1 , mxxnode = 0;
for(int i=0; i<sze; i++)
{
v = adj[u][i];
if(mxx < units[v])
{
mxx = units[v];
mxxnode = v;
}
}
if(!visited[mxxnode])
{
visited[mxxnode] = 1;
Q.push(mxxnode);
}
}
}
int main()
{
fast;
int u , v , i, N , M , test , caseno = 0;
sf("%d", &test);
while(test--)
{
CLR();
cin >> N >> M;
for(i=0; i<N; i++)
{
cin >> units[i];
}
for(i=0; i<M; i++)
{
cin >> u >> v;
adj[u].psb(v);
}
BFS(0);
cout << "Case " << ++caseno << ": " << total << " " << lastnode << "\n";
}
return 0;
}
/*
6 6
0 8 9 2 7 5
5 4
5 3
1 5
0 1
0 2
2 1
*/
/*
Algorithm: BFS
Just take the maximum adjacent nodes and calculate the units of it as well as keep
track about the last node where you are terminating your graph traverse.
*/
#include<bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define psb push_back
#define fast ios_base::sync_with_stdio(false)
const int high = 205;
int units[high], visited[high], total = 0;
int mxx = -1 , mxxnode = 0, lastnode = 0;
vector<int>adj[high];
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clear();
units[i] = 0;
visited[i] = 0;
}
total = 0;
}
void BFS(int s)
{
visited[s] = 1;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
lastnode = u;
total += units[u];
Q.pop();
int sze = adj[u].size(), v;
mxx = -1 , mxxnode = 0;
for(int i=0; i<sze; i++)
{
v = adj[u][i];
if(mxx < units[v])
{
mxx = units[v];
mxxnode = v;
}
}
if(!visited[mxxnode])
{
visited[mxxnode] = 1;
Q.push(mxxnode);
}
}
}
int main()
{
fast;
int u , v , i, N , M , test , caseno = 0;
sf("%d", &test);
while(test--)
{
CLR();
cin >> N >> M;
for(i=0; i<N; i++)
{
cin >> units[i];
}
for(i=0; i<M; i++)
{
cin >> u >> v;
adj[u].psb(v);
}
BFS(0);
cout << "Case " << ++caseno << ": " << total << " " << lastnode << "\n";
}
return 0;
}
/*
6 6
0 8 9 2 7 5
5 4
5 3
1 5
0 1
0 2
2 1
*/
Comments
Post a Comment