Can anyone tell me the tricky part of this problem or is there any mistake with my code?
I turn all edges to the opposite side and run the Dijkstra Algorithm to know the shortest path from every vertex to the exit.
The following is my code
Thanks~
Code: Select all
/*1112*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
vector<vii> v;
int num,ex,time,edge;
int ans;
priority_queue<ii> pq;
bool vis[105];
void SSSP()
{
pq.push(ii(0,ex));
vis[ex]=1;
ans++;
while (!pq.empty())
{
ii front=pq.top();pq.pop();
int A=front.first,B=front.second;
for (int i=0;i<v[B].size();i++)
{
ii next=v[B][i];
int na=next.first,nb=next.second;
if (!vis[nb]&&A+na<=time)
{
pq.push(ii(A+na,nb));
vis[nb]=1;
ans++;
}
}
}
}
int main()
{
bool first=1;
int amm;
scanf("%d",&amm);
while (amm--)
{
scanf("%d%d%d%d",&num,&ex,&time,&edge);
ex--;
v.assign(num,vii());
ans=0;
int a,b,c;
for (int i=0;i<edge;i++)
{
scanf("%d%d%d",&a,&b,&c);
v[b-1].push_back(ii(c,a-1));
}
for (int i=0;i<num;i++)
{
sort(v[i].begin(),v[i].end());
}
memset(vis,false,sizeof vis);
SSSP();
if (first)first=0;
else puts("");
printf("%d\n",ans);
}
return 0;
}