题目:
思路:
把优先队列的做法改个数据范围就可以了
AC代码:
#include<cstdio>
#include <iostream>
#include<cstring>
#include<algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=111111;
struct node
{
int x,dis;
bool operator >(const node &a)const
{
if(dis==a.dis) return x<a.x;
return dis>a.dis;
}
};
priority_queue <node,vector<node>,greater<node>> q;
typedef pair<int,int> pii;
vector <pii> e[2*maxn];
int n,m,s,day[maxn],dis[maxn];
bool vis[maxn];
int main()
{
cin>>n>>m>>s;
for(int i=1;i<=m;i++)
{
int x,y,w;
cin>>x>>y>>w;
if(x==y) continue;
e[x].push_back({y,w});
}
memset(dis,0x3f,sizeof(dis));
dis[s]=0;
q.push({s,0});
while(!q.empty())
{
int temp=q.top().x;
q.pop();
if(vis[temp]) continue;
vis[temp]=1; //cout<<"!!"<<temp<<endl;
for(auto to:e[temp])
{
dis[to.first]=min(dis[to.first],dis[temp]+to.second);
//cout<<"to.first="<<to.first<<" "<<"dis[to.first]"<<dis[to.first]<<endl;
q.push({to.first,dis[to.first]});
}
}
//for(int i=1;i<=n;i++) if(!vis[i]) dis[i]=anss;
for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
cout<<endl;
return 0;
}