problem link:
Discuss: in this problem you can simply use dijksta and solve it. jut loop through 1 to n and call dijkstra every time.if distant of exit node is less or equal then timer then make count increase. there is no tricky part in this problem.
try yourself , before see the code
#include<bits/stdc++.h>
#define mx 1e9
using namespace std;
typedef pair<int,int>pr;
vector<pr>vec[1000];
int distan[1000];
bool visited[1000];
int dijkstra(int src)
{
distan[src]=0;
priority_queue<pr,std:: vector<pr>,std::greater<pr> >que;
que.push({0,src});
while(!que.empty())
{
int v=que.top().second;
int d=que.top().first;
que.pop();
if(visited[v])
continue;
visited[v]=1;
int sz=vec[v].size();
for(int i=0;i<sz;i++)
{
int u=vec[v][i].first;
int w=vec[v][i].second;
if(distan[v]+w<distan[u])
{
distan[u]=distan[v]+w;
que.push({distan[u],u});
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin >>t;
for(int l=0;l<t;l++)
{
int n,e,T,a,b,w,i,j,m,cnt=0;
cin >>n>>e>>T;
cin >>m;
for(i=0;i<m;i++)
{
cin >>a>>b>>w;
vec[a].push_back({b,w});
}
for(i=0;i<=n;i++)
{
fill(visited,visited+n+100,0);
fill(distan,distan+n+100,mx);
dijkstra(i);
if(distan[e]<=T)
{
cnt++;
}
}
cout <<cnt<<endl;
if(l!=t-1)
cout <<endl;
for(i=0;i<n+5;i++)
vec[i].clear();
}
return 0;
}
Discuss: in this problem you can simply use dijksta and solve it. jut loop through 1 to n and call dijkstra every time.if distant of exit node is less or equal then timer then make count increase. there is no tricky part in this problem.
try yourself , before see the code
#include<bits/stdc++.h>
#define mx 1e9
using namespace std;
typedef pair<int,int>pr;
vector<pr>vec[1000];
int distan[1000];
bool visited[1000];
int dijkstra(int src)
{
distan[src]=0;
priority_queue<pr,std:: vector<pr>,std::greater<pr> >que;
que.push({0,src});
while(!que.empty())
{
int v=que.top().second;
int d=que.top().first;
que.pop();
if(visited[v])
continue;
visited[v]=1;
int sz=vec[v].size();
for(int i=0;i<sz;i++)
{
int u=vec[v][i].first;
int w=vec[v][i].second;
if(distan[v]+w<distan[u])
{
distan[u]=distan[v]+w;
que.push({distan[u],u});
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin >>t;
for(int l=0;l<t;l++)
{
int n,e,T,a,b,w,i,j,m,cnt=0;
cin >>n>>e>>T;
cin >>m;
for(i=0;i<m;i++)
{
cin >>a>>b>>w;
vec[a].push_back({b,w});
}
for(i=0;i<=n;i++)
{
fill(visited,visited+n+100,0);
fill(distan,distan+n+100,mx);
dijkstra(i);
if(distan[e]<=T)
{
cnt++;
}
}
cout <<cnt<<endl;
if(l!=t-1)
cout <<endl;
for(i=0;i<n+5;i++)
vec[i].clear();
}
return 0;
}
No comments:
Post a Comment