NC15108 道路建设
\(MST\)裸题
const int N=110;
int g[N][N];
int dist[N];
bool vis[N];
int money;
int n,m;
int prim()
{
memset(dist,0x3f,sizeof dist);
memset(vis,0,sizeof vis);
dist[1]=0;
int res=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
if(!vis[j] && (t==-1 || dist[t]>dist[j]))
t=j;
if(dist[t] == INF) return -1;
vis[t]=true;
res+=dist[t];
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],g[t][j]);
}
return res;
}
int main()
{
while(cin>>money>>m>>n)
{
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b]=min(g[a][b],c);
}
int t=prim();
if(~t && t<=money) puts("Yes");
else puts("No");
}
//system("pause");
}