Pagini recente » Cod sursa (job #1735848) | Cod sursa (job #2897084) | Cod sursa (job #264567) | Cod sursa (job #1600529) | Cod sursa (job #1521551)
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#define MMAX 100007
#define PII pair<int, int>
#define pb push_back
using namespace std;
int n, m, x, y, z, sze, update;
double ans;
vector <PII> adj[MMAX];
PII tmp;
struct dfp
{
int lung;
int nod;
int prob;
} aux, curr;
stack <dfp> dfs;
inline void compute_dfs()
{
aux.lung = 0;
aux.nod = 1;
aux.prob = 1;
dfs.push(aux);
while(!dfs.empty())
{
curr = dfs.top();
dfs.pop();
if(curr.lung >= 1000) continue;
if(curr.prob > 100000000) continue;
if(curr.nod == n)
{
ans += ((double)curr.lung/(double)curr.prob);
continue;
}
sze = adj[curr.nod].size();
update = curr.prob * sze;
for(int i = 0; i< sze; ++i)
{
aux.lung = curr.lung + adj[curr.nod][i].second;
aux.nod = adj[curr.nod][i].first;
aux.prob = update;
dfs.push(aux);
}
}
}
int main()
{
freopen("tunel.in", "r", stdin);
freopen("tunel.out", "w", stdout);
scanf("%d %d", &n, &m);
for(; m; --m)
{
scanf("%d%d%d", &x, &y, &z);
tmp.second = z;
tmp.first = y;
adj[x].pb(tmp);
swap(tmp.first, x);
adj[x].pb(tmp);
}
compute_dfs();
printf("%lf\n", ans);
return 0;
}