主要用到迪杰斯特拉找最小环,堆优化。
在这种实现方式下,每个节点最多入堆一次,出堆一次,更新堆中节点的距离一次,因此时间复杂度主要由堆的操作决定。总的时间复杂度为O((V+E)logV),其中V是节点数量,E是边的数量。
值得注意的是,堆优化的Dijkstra算法适用于稀疏图,即边的数量相对较小的情况下效果较好。对于稠密图,即边的数量接近于V^2的情况下,使用堆优化的Dijkstra算法可能不如普通的Dijkstra算法效率高。
char x;
const int M = 5e4+10;
vector<pair<ll, ll>> g[M];
ll n, m, c;
long long dis[M], vis[M];
long long res = 1e18;
void dij(int st)
{
for (int i = 0; i <= n; i++)
{
dis[i] = 1e18;
vis[i] = 0;
}
dis[st] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll,ll>>> q;
q.push({ 0,st });
while (!q.empty())
{
ll tmp = q.top().second;
q.pop();
if (vis[tmp] == 1) continue;
vis[tmp] = 1;
for (auto it : g[tmp])
{
ll y = it.first, w = it.second;
if (y == st) res = min(res, dis[tmp] + w);
if (dis[y] > dis[tmp] + w)
{
dis[y] = dis[tmp] + w;
q.push({ dis[y], y });
}
}
/*for (int i = 0; i < g[tmp].size(); i++)
{
if (dis[g[tmp][i].first] > g[tmp][i].second + dis[tmp])
{
dis[g[tmp][i].first] = g[tmp][i].second + dis[tmp];
q.push({ dis[g[tmp][i].first] ,g[tmp][i].first });
}
}*/
}
}
int main()
{
cin >> n >> m >> c;
int flag = 0;
for (int i = 1; i <= m; i++)
{
ll x, y, z;
cin >> x >> y >> z;
g[x].push_back({ y,z });
if (c >= z) flag = 1;
}
if (!flag)
{
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; i++)
{
dij(i);
}
if (c >= res)
{
cout << 2 << endl;
}
else
cout << 1 << endl;
return 0;
}