Code: Select all
#include <iostream>
#include <cstdio>
#include "vector"
#include "queue"
#include "cstring"
#include "algorithm"
//N, maximum number of node
#define N 100
using namespace std;
int n,m;
vector< int > life[N+10];
int learn[N+10]; // Learning at each node
int total[N+10]; // Total learning from source
void takeInput()
{
//Not handle Blank line before each test case scanf will do that for me ( I think ).
for(int i=0; i<n; i++) {
scanf("%d", &learn[i]);
life[i].clear();
}
for(int i=0, u, v; i<m; i++) {
scanf("%d %d", &u, &v);
life[u].push_back(v);
}
return;
}
void maxLearn()
{
// Find maximum learning from source to each node and store it in total( Algo : BFS)
queue< int > Q;
memset(total, 0, sizeof(total));
int u,v;
Q.push(0);
while( !Q.empty() ) {
u = Q.front(); Q.pop();
for(int i=0, l=life[u].size(); i<l; i++) {
v = life[u][i];
if(total[v] < (total[u]+learn[v]) ) {
total[v] = total[u] + learn[v];
Q.push(v);
}
}
}
return;
}
int main()
{
//freopen("a.txt", "r", stdin);
int *max_e;
int kases; cin >>kases;
for(int kase=1; kase<=kases; kase++) {
scanf("%d %d", &n, &m);
takeInput();
maxLearn();
max_e = max_element(total, total+n);
printf("Case %d: %d %d\n", kase, *max_e, (max_e-total) );
}
return 0;
}