Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.8 kB
1
Indexable
Never
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define int long long
#define all(a) begin(a),end(a)
#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
const int mod=998244353;
const int inf=1e18;
const int MAXN=100005;

vector<pair<int,pii>> Edge[MAXN],revEdge[MAXN];
vector<pii> edge;
int d1[MAXN],d2[MAXN],L[MAXN];

void solve(){
	memset(d1,0x3f,sizeof(d1));
	memset(d2,0x3f,sizeof(d2));
	int n,m;
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int u,v,l;
		cin >> u >> v >> l;
		Edge[u].pb({v,{l,i}});
		revEdge[v].pb({u,{l,i}});
		edge.pb({u,v});
		L[i]=l;
	}
	priority_queue<pii> q;
	q.push({0,1});
	d1[1]=0;
	while(!q.empty()){
		pii now=q.top();q.pop();
		if(d1[now.S]<now.F) continue;

		for(auto i:Edge[now.S]){
			if(d1[now.S]+i.S.F<d1[i.F]){
				d1[i.F]=d1[now.S]+i.S.F;
				q.push({d1[i.F],i.F});
			}
		}
	}

	q.push({0,2});
	d2[2]=0;
	while(!q.empty()){
		pii now=q.top();q.pop();
		if(d2[now.S]<now.F) continue;

		for(auto i:revEdge[now.S]){
			if(d2[now.S]+i.S.F<d2[i.F]){
				d2[i.F]=d2[now.S]+i.S.F;
				q.push({d2[i.F],i.F});
			}
		}
	}
	int ori=d1[2];
	for(int i=0;i<m;i++){
		int len=d1[edge[i].S]+d2[edge[i].F]+L[i];
		if(len<ori) cout << "HAPPY\n";
		else if(len==ori) cout << "SOSO\n";
		else{
			queue<int> qq;
			qq.push(1);
			bool can_reach=false;
			while(!qq.empty()){
				int now=qq.front();qq.pop();
				if(now==2){
					can_reach=true;
					break;
				}
				for(auto j:Edge[now]){
					
					if(j.S.S==i || d1[now]+j.S.F!=d1[j.F]) continue;
					qq.push(j.F);
				}
			}
			if(can_reach) cout << "SOSO\n";
			else  cout << "SAD\n";
		}
	}
}


signed main(){
	cin.sync_with_stdio(0),cin.tie(0);
	int N=1;
	//cin >> N;
	for(int i=1;i<=N;i++){
		solve();
	}
}