ICPC2017 Asia Regional Tsukuba F Pizza Delivery

問題

重み付きの有向グラフが与えられる。i番目の辺の向きを反転させたとき、1番から2番の頂点への最短路が

  • 短くなる
  • 変化しない
  • 長くなる

のいずれであるかをそれぞれ出力せよ。

制約条件

グラフの辺と頂点≦10万
辺の重みは正整数で10万以下

解法

辺(i, j, w)を反転したとき最短路が短くなるかどうかはすぐわかる。
スタートから各頂点への最短距離dist_sおよび、各頂点からゴールへの最短距離dist_tを求めておいて、dist_s[j] + w + dist_t[i]が元の最短距離より短くなっているかを見ればよい。


辺を反転したとき長くなるのは、その辺がスタートからゴールへの最短路であり、別ルートの最短路がないとき。
これはどういうことかというと、最短路に使う辺だけを抜き出してきて無向グラフにしたときに橋になっているときである。


以上のどちらでもないときは変化しない。

ソースコード

int n, m;
ll dist_s[111111], dist_t[111111];
int ans[111111], low[111111], num[111111], N;
vector<vector<tuple<int,int,int>>> e, re, g;

void rec(int cur, int pid){
	num[cur] = low[cur] = ++N;
	vector<tuple<int,int,int>> es;
	for(auto p : g[cur]){
		int to, cost, id; tie(to, cost, id) = p;
		if(id == pid) continue;
		if(num[to] == 0){
			rec(to, id);
			low[cur] = min(low[cur], low[to]);
			es.emplace_back(p);
		}
		else low[cur] = min(low[cur], num[to]);
	}
	for(auto p : es){
		int to, cost, id; tie(to, cost, id) = p;
		if(low[to] > num[cur]) ans[id] = 1;
	}
}

int main(){
	cin >> n >> m;
	e.resize(n); re.resize(n); g.resize(n);
	rep(i, m){
		int a, b, c; cin >> a >> b >> c; a--; b--;
		e[a].emplace_back(b, c, i);
		re[b].emplace_back(a, c, i);
	}
	auto dij = [](ll *dist, int s, const vector<vector<tuple<int,int,int>>> &e){
		priority_queue<pair<ll,int>> q;
		q.emplace(0, s);
		rep(i, n) dist[i] = 1e18;
		dist[s] = 0;
		
		while(!q.empty()){
			int c; ll co;
			tie(co, c) = q.top(); q.pop();
			if(dist[c] < -co) continue;
			
			for(auto to : e[c]){
				ll nco = -co + get<1>(to);
				if(dist[get<0>(to)] <= nco) continue;
				dist[get<0>(to)] = -co + get<1>(to);
				q.emplace(co - get<1>(to), get<0>(to));
			}
		}
	};
	dij(dist_s, 0, e);
	dij(dist_t, 1, re);
	{
		ll dist = dist_s[1];
		rep(i, n) for(auto p : e[i]){
			int to, cost, id; tie(to, cost, id) = p;
			if(dist_s[i] + cost + dist_t[to] == dist){
				g[i].emplace_back(to, cost, id);
				g[to].emplace_back(i, cost, id);
			}
			else{
				ans[id] = dist_s[to] + cost + dist_t[i] < dist ? -1 : 0;
			}
		}
		rec(0, -1);
		rep(i, m){
			if(ans[i] < 0) cout << "HAPPY" << endl;
			else cout << (ans[i] == 0 ? "SOSO" : "SAD") << endl;
		}
	}
	return 0;
}