Codeforces Round #364 (Div. 2) F. Break Up(tarjan+暴力)

F. Break Up

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible.

There are n towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads.

Towns s and t announce the final break of any relationship and intend to rule out the possibility of moving between them by the roads. Now possibly it is needed to close several roads so that moving from s to t using roads becomes impossible. Each town agrees to spend money on closing no more than one road, therefore, the total number of closed roads will be no more than two.

Help them find set of no more than two roads such that there will be no way between s and t after closing these roads. For each road the budget required for its closure was estimated. Among all sets find such that the total budget for the closure of a set of roads is minimum.

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 30 000) — the number of towns in Berland and the number of roads.

The second line contains integers s and t (1 ≤ s, t ≤ ns ≠ t) — indices of towns which break up the relationships.

Then follow m lines, each of them contains three integers x iy i and w i (1 ≤ x i, y i ≤ n, 1 ≤ w i ≤ 109) — indices of towns connected by the i-th road, and the budget on its closure.

All roads are bidirectional. It is allowed that the pair of towns is connected by more than one road. Roads that connect the city to itself are allowed.

Output

In the first line print the minimum budget required to break up the relations between s and t, if it is allowed to close no more than two roads.

In the second line print the value c (0 ≤ c ≤ 2) — the number of roads to be closed in the found solution.

In the third line print in any order c diverse integers from 1 to m — indices of closed roads. Consider that the roads are numbered from 1 to m in the order they appear in the input.

If it is impossible to make towns s and t disconnected by removing no more than 2 roads, the output should contain a single line -1.

If there are several possible answers, you may print any of them.

Examples

input

Copy

6 7
1 6
2 1 6
2 3 5
3 4 9
4 6 4
4 6 5
4 5 1
3 1 3

output

Copy

8
2
2 7

input

Copy

6 7
1 6
2 3 1
1 2 2
1 3 3
4 5 4
3 6 5
4 6 6
1 5 7

output

Copy

9
2
4 5

input

Copy

5 4
1 5
2 1 3
3 2 1
3 4 4
4 5 2

output

Copy

1
1
2

input

Copy

2 3
1 2
1 2 734458840
1 2 817380027
1 2 304764803

output

Copy

-1

題目大意:

N(1<=N<=1e3)個點,M(1<=M<=4e3)條邊的帶邊權無向圖,刪除一條邊的代價爲該邊邊權,你要找到一個最小代價,使得S和T不連通,輸出代價、刪除的邊數、邊的編號,刪除的數量要<=2。

沒想到怎麼寫T_T,好不容易看懂做法,還是個碼農題,感覺很多暴力的圖論題都和刪邊刪點有關吧。

解法:

刪除邊數爲0:S與T一定不連通,判一下就好。

刪除邊數爲1:S與T之間有一條割邊,找到該割邊的最小權值。

刪除邊數爲2:S到T只有兩條路徑,求兩路徑的最小值之和。

0是單獨判的,1和2都得求,因爲可能刪除兩條邊比刪除一條邊更優。我們先找到一條S到T的路徑,假設該路徑爲L1,枚舉刪除L1上的邊,從S到T記錄第二條路徑L2,如果這時候無法從S到T,那麼刪除的邊一定是割邊,更新下答案;否則求一下整張圖的割邊,如果割邊存在於L2中,那就說明當前L1中枚舉刪除的邊與L2中的一條割邊可以使S無法到達T,若不存在割邊,此時無解。

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 1e3 + 100;
const int M = 3e4 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

vector <pir> G[N << 1];
vector <int> g;
int dfn[N], low[N], n, m, cnt;
int val[M], sp, tp;
int edge[M], res;
bool vis[N], cut[M];
int ID[5];
ll ans;

bool path(int x) {   // 找到L1路徑,同時判斷S與T是否連聯通
	if (x == tp)
		return true;
	vis[x] = true;
	for (auto it : G[x]) {
		int v = it.first, id = it.second;
		if (!vis[v] && path(v)) {
			edge[++res] = id;
			return true;
		}
	}
	return false;
}
bool dfs(int x, int no) {   // 找到L2路徑,no表示當前刪除的邊
	if (x == tp)
		return true;
	vis[x] = true;
	for (auto it : G[x]) {
		int v = it.first;
		int id = it.second;
		if (id != no && !vis[v] && dfs(v, no)) {
			g.push_back(id);
			return true;
		}
	}
	return false;
}
void tarjan(int x, int fa, int no) {  // 求割邊,no表示當前刪除的邊,fa表示上一次走的邊編號
	dfn[x] = low[x] = ++cnt;
	for (auto it : G[x]) {
		int v = it.first, id = it.second;
		if (id == fa || id == no)
			continue;
		if (!dfn[v]) {
			tarjan(v, id, no);
			Min(low[x], low[v]);
			if (low[v] > dfn[x])
				cut[id] = true;   // 割邊
		}
		else
			Min(low[x], dfn[v]);
	}
}

int main()
{
	cin >> n >> m >> sp >> tp;
	for (int i = 1; i <= m; i++) {
		int u, v;
		sc("%d %d %d", &u, &v, &val[i]);  
		G[u].push_back({ v, i });
		G[v].push_back({ u, i });
	}

	// 找任意一條路徑
	if (!path(sp))          // 第一種情況
		printf("0\n0\n"), exit(0);

	// 枚舉刪除第一條路徑的邊
	ans = LINF;
	for (int i = 1; i <= res; i++) {
		int ii = edge[i]; // 當前刪除的編號
		MEM(vis, 0), g.clear();
		if (!dfs(sp, ii)) {          // 第二種情況,刪除該邊不連通
			if (val[ii] < ans)
				ans = val[ii], ID[1] = ii, ID[2] = 0;
		}
		else {
			cnt = 0;
			MEM(dfn, 0), MEM(low, 0);
			MEM(cut, 0);
			tarjan(sp, 0, ii);

			// 是否有割邊
			for (auto it : g) {
				if (cut[it] && val[it] + val[ii] < ans)
					ans = val[it] + val[ii], ID[1] = ii, ID[2] = it;
			}
		}
	}

	if (ans == LINF)
		printf("-1\n");
	else {
		printf("%d\n", ans);
		if (!ID[2])
			printf("1\n%d\n", ID[1]);
		else
			printf("2\n%d %d\n", ID[1], ID[2]);
	}
	return 0;  // 改數組大小!!!用pair記得改宏定義!!!
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章