题目
题意: 题目很长,最后看了半天的意思就是有向图求最短路,保证没有负环。
思路: spfa
时间复杂度: O(n*m)
代码:
#include<bits/stdc++.h>
using namespace std;
#define mem(a,x) memset(a,x,sizeof(a))
#define fir(i,a,b) for(int i=a;i<=b;++i)
const int INF = 0x3f3f3f3f;
const int N = 2002;
int a[N][N];
int n,m,k,T;
int dist[N];
bool vis[N];
int limit;
int in[N];
void init()
{
fir(i,1,n)
{
fir(j,1,n)
{
if(i==j) a[i][j] = 0;
else a[i][j] = INF;
}
}
}
void spfa(int st)
{
mem(dist,0x3f);
mem(vis,false);
dist[st] = 0;
queue<int> q;
q.push(st);
while(q.size())
{
int u = q.front(); q.pop();
vis[u] = 0;
cout<<u<<":"<<dist[u]<<"?"<<endl;
for(int i=1;i<=n;++i)
{
if(i == u) continue;
// cout<<u<<" "<<i<<":"<<a[u][i]<<endl;
if(a[u][i]!=INF && dist[i] > dist[u] + a[u][i])
{
dist[i] = dist[u] + a[u][i];
if(!vis[i])
{
vis[i] = 1;
q.push(i);
}
}
}
}
}
void solve()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
init();
for(int i=1;i<=n;++i)
{
int num; cin>>num;
while(num--)
{
int x,w;
cin>>x>>w; w *= -1;
in[x]++;
a[i][x] = w;
}
}
cin>>limit;
int st;
for(int i=1;i<=n;++i) if(in[i] == 0) st = i;
spfa(st);
for(int i=1;i<=n;++i)
{
if(dist[i] <= limit)
{
cout<<i<<"\n";
}
}
}
signed main(void)
{
solve();
return 0;
}