2WA
第一次错在inf太小,20亿足够
第二次错在eque2清空的位置放错了,导致没桥的时候会误判为有桥
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=1005;
const int maxm=60005;
const int inf=0x3f3f3f3f;
struct ee {
int to, next, cost, id;
bool cut, valid;
} edge[maxm];
int head[maxn], tot;
int low[maxn], dfn[maxn];
int Index, bridge;
int n, m, s, t;
vector<ee> eque, eque2;
void addedge(int u, int v, int w) {
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].cost=w;
edge[tot].cut=false;
edge[tot].valid=true;
edge[tot].id=tot;
head[u]=tot++;
}
void inittarjan() {
for (int i=0; i<tot; i++)
edge[i].cut=false;
Index=bridge=0;
memset(dfn, 0, sizeof dfn);
}
void tarjan(int u, int pre) {
low[u]=dfn[u]=++Index;
int precnt=0;
for (int i=head[u]; i!=-1; i=edge[i].next) {
int v=edge[i].to;
if (!edge[i].valid)
continue;
if (v==pre&&precnt==0) {
precnt++;
continue;
}
if (!dfn[v]) {
tarjan(v, u);
if (low[u]>low[v])
low[u]=low[v];
if (low[v]>dfn[u]) {
bridge++;
edge[i].cut=true;
edge[i^1].cut=true;
}
} else if (low[u]>dfn[v])
low[u]=dfn[v];
}
}
void print() {
for (int i=0; i<n; i++) {
printf("%d: ", i);
for (int j=head[i]; j!=-1; j=edge[j].next)
printf("%d ", edge[j].to);
puts("");
}
}
void printroute() {
for (int i=eque.size()-1; i>=0; i--)
printf("%d ", eque[i].to);
puts("");
}
void printbridge() {
for (int i=0; i<tot; i++)
printf("edge[%d]=%d\n", i, edge[i].cut);
}
bool vis[maxn], vis2[maxn];
bool dfs(int u, int dest) {
if (u==dest)
return true;
vis[u]=true;
for (int i=head[u]; i!=-1; i=edge[i].next)
if (!vis[edge[i].to]&&edge[i].valid)
if (dfs(edge[i].to, dest)) {
eque.push_back(edge[i]);
return true;
}
return false;
}
bool dfs2(int u, int dest) {
if (u==dest)
return true;
vis2[u]=true;
for (int i=head[u]; i!=-1; i=edge[i].next)
if (!vis2[edge[i].to]&&edge[i].valid)
if (dfs2(edge[i].to, dest)) {
eque2.push_back(edge[i]);
return true;
}
return false;
}
void solve() {
memset(vis, 0, sizeof vis);
eque.clear();
if (!dfs(s, t)) {
puts("0\n0");
return;
}
int res=(inf<<1);
eque2.clear();
bool flag1=false, flag2=false;
int resid1=-1, resid2=-1;
for (unsigned int i=0; i<eque.size(); i++) {
memset(vis2, 0, sizeof vis2);
int id=eque[i].id;
edge[id].valid=false;
edge[id^1].valid=false;
eque2.clear();
if (!dfs2(s, t)) {
if (edge[id].cost<res) {
flag1=true;
flag2=false;
res=edge[id].cost;
resid1=(id>>1)+1;
}
} else {
inittarjan();
tarjan(s, -1);
for (unsigned int j=0; j<eque2.size(); j++)
if (edge[eque2[j].id].cut) {
int add=edge[id].cost+edge[eque2[j].id].cost;
if (add<res) {
flag1=false;
flag2=true;
res=add;
resid1=(id>>1)+1;
resid2=(eque2[j].id>>1)+1;
}
}
}
edge[id].valid=true;
edge[id^1].valid=true;
}
if (res==(inf<<1))
puts("-1");
else if (flag1==true&&flag2==false)
printf("%d\n1\n%d\n", res, resid1);
else if (flag2==true&&flag1==false)
printf("%d\n2\n%d %d\n", res, resid1, resid2);
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d%d%d%d", &n, &m, &s, &t);
s--;
t--;
tot=0;
memset(head, -1, sizeof head);
for (int i=0; i<m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a--;
b--;
addedge(a, b, c);
addedge(b, a, c);
}
solve();
return 0;
}