Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Aug 10, 2019
1 parent a14268f commit 6a1b811
Show file tree
Hide file tree
Showing 6 changed files with 5,254 additions and 1 deletion.
2 changes: 1 addition & 1 deletion myCpps/!-OJcreater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace std;

//***************************************
const string CPPfile = "travelInDesert_UVA10816"; //*
const string CPPfile = "aWalkThroughTheForest_UVA10917"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
Expand Down
98 changes: 98 additions & 0 deletions myCpps/aWalkThroughTheForest_UVA10917.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <bits/stdc++.h>

using namespace std;

ifstream fin("aWalkThroughTheForest_UVA10917.in");
ofstream fout("aWalkThroughTheForest_UVA10917.out");

using ll = long long;
const ll inf = LLONG_MAX / 2;

struct segment
{
ll u;
ll d;

bool operator < (const segment & temp) const
{
return d > temp.d;
}
};
segment _segment(ll u, ll d)
{
segment temp{u, d}; return temp;
}

ll count(ll u, vector<ll> & dp, vector<ll> & dis, vector<vector<ll>> & g)
{
if (u == 2)
{
return 1;
}
else if (dp[u] != -1)
{
return dp[u];
}
else
{
ll ans = 0;

ll size = g[u].size();
for (ll i = 0; i <= size - 1; ++i)
{
ll v = g[u][i];

if (dis[v] < dis[u])
{
ans += count(v, dp, dis, g);
}
}

dp[u] = ans;
return ans;
}
}

int main()
{
while (true)
{
ll n = 0; fin >> n;
if (n == 0) break;

ll m; fin >> m;

vector<vector<ll>> a(n + 1, vector<ll>(n + 1, inf)),
g(n + 1);
for (ll c = 1; c <= m; ++c)
{
ll u, v, d; fin >> u >> v >> d;

a[u][v] = d; a[v][u] = a[u][v];
g[u].push_back(v); g[v].push_back(u);
}

vector<ll> dis(n + 1, inf);
priority_queue<segment> pq; pq.push(_segment(2, 0));
while (pq.empty() == false)
{
segment now = pq.top(); pq.pop();

if (now.d >= dis[now.u]) continue;
dis[now.u] = now.d;

ll size = g[now.u].size();
for (ll i = 0; i <= size - 1; ++i)
{
ll v = g[now.u][i];

pq.push(_segment(v, now.d + a[now.u][v]));
}
}

vector<ll> dp(n + 1, -1);
fout << count(1, dp, dis, g) << '\n';
}

return 0;
}
Loading

0 comments on commit 6a1b811

Please sign in to comment.