Cod sursa(job #403828)

Utilizator mihaionlyMihai Jiplea mihaionly Data 25 februarie 2010 13:16:33
Problema Traseu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
using namespace std;
#define nmax 65
#define inf 0x3f3f3f
#define ll long long
#define min(a,b) ((a<b)?(a):(b))
ifstream f("traseu.in");
ofstream g("traseu.out");
int n,m;
int C[nmax][nmax];
ll drum[nmax],sol;
bool viz[nmax];
int Q[1000];
#define Q (Q-1)
bool eok;
struct graf
 {
 int x;
 graf *urm;
 };
graf *G[nmax];
void add(graf *&g,int x)
 {
 graf *p=new graf;
 p->x=x;
 p->urm=g;
 g=p;
 }
void read()
 {
 int i,x,y,c;
 f>>n>>m;
 for(i=1;i<=m;i++)
  {
  f>>x>>y>>c;
  add(G[x],y);
  C[x][y]=c;
  }
 }
void dfs(int nod,ll cost)
 {
 viz[nod]=true;
 for(graf *g=G[nod];g!=NULL;g=g->urm)
  {
  if(!viz[g->x])
   {
   viz[g->x]=true;
   dfs(g->x,cost+C[nod][g->x]);
   }
  if(g->x==1)
   sol+=cost+C[nod][g->x];
  }
 }
int main()
 {
 read();
 dfs(1,0);
 g<<sol;
 }