题面保证数据随机,所以乱搞就好
直接忽略选取任意一个的条件,最短路记录前驱
……然后就做完了吗?
- 当出现类似 \(n=1e5, m=2e5\) 的时候数组尤其容易开小,应该开 \(4e5\) 而不是 \(2e5\)
- 写暴力的时候注意memset整个数组可能就T了
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define N 400010
#define ll long long
#define fir first
#define sec second
#define make make_pair
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m;
int col[N], head[N], size=1, fa[N];
bool able[N];
struct edge{int to, next; ll val; bool liv;}e[N<<1];
inline void add(int s, int t, ll w) {e[++size].to=t; e[size].val=w; e[size].next=head[s]; head[s]=size;}
inline int find(int p) {return fa[p]==p?p:fa[p]=find(fa[p]);}
namespace force{
ll ans=INF, mdis[N], dis[N];
bool vis[N];
priority_queue<pair<ll, int>> q;
void dij(int s) {
// cout<<"dij: "<<s<<endl;
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
while (q.size()) q.pop();
dis[s]=0; q.push(make(0, s));
pair<ll, int> u;
while (q.size()) {
u=q.top(); q.pop();
if (vis[u.sec]) continue;
vis[u.sec]=1;
for (int i=head[u.sec],v; ~i; i=e[i].next) if (e[i].liv) {
v = e[i].to;
if (dis[u.sec]+e[i].val < dis[v]) {
dis[v] = dis[u.sec]+e[i].val;
q.push(make(-dis[v], v));
}
}
}
// cout<<"dis: "; for (int i=1; i<=n; ++i) cout<<dis[i]<<' '; cout<<endl;
}
void init() {
// cout<<"init"<<endl;
memset(mdis, 0x3f, sizeof(mdis));
for (int i=2; i<=m*2+1; ++i) e[i].liv=1;
for (int i=1; i<=n; ++i) if (!col[i]) {
dij(i);
for (int j=1; j<=n; ++j) if (col[j]) mdis[i]=min(mdis[i], dis[j]);
}
// cout<<"mdis: "; for (int i=1; i<=n; ++i) cout<<mdis[i]<<' '; cout<<endl;
}
bool check() {
// cout<<"check"<<endl;
for (int i=1; i<=n; ++i) if (!col[i]) {
// cout<<"i: "<<i<<endl;
dij(i);
ll minn=INF;
for (int j=1; j<=n; ++j) if (col[j]) minn=min(minn, dis[j]);
if (minn!=mdis[i]) return 0;
}
return 1;
}
void solve() {
init();
int lim=1<<m;
for (int s=0; s<lim; ++s) {
ll sum=0;
for (int i=0,t; i<m; ++i) {
t=(i+1)<<1;
if (s&(1<<i)) {
e[t].liv=e[t^1].liv=1;
sum+=e[t].val;
}
else {
e[t].liv=e[t^1].liv=0;
}
}
if (sum<ans && check()) ans=min(ans, sum);
}
printf("%lld\n", ans);
exit(0);
}
}
namespace task1{
ll ans, dis[N];
int sta[N], top, back[N];
bool vis[N], insta[N], keep[N];
priority_queue<pair<ll, int>> q;
void dij(int s) {
while (q.size()) q.pop();
dis[s]=0; q.push(make(0, s));
sta[++top]=s; insta[s]=1;
pair<ll, int> u;
while (q.size()) {
u=q.top(); q.pop();
if (vis[u.sec]) continue;
vis[u.sec]=1;
if (col[u.sec]) {
int now=u.sec;
do {
keep[back[now]]=keep[back[now]^1]=1;
now=e[back[now]].to;
} while (now!=s);
while (top) {
dis[sta[top]]=INF; vis[sta[top]]=0; insta[sta[top]]=0; back[sta[top]]=0;
--top;
}
return ;
}
for (int i=head[u.sec],v; ~i; i=e[i].next) {
v = e[i].to;
if (dis[u.sec]+e[i].val < dis[v]) {
dis[v] = dis[u.sec]+e[i].val; back[v]=i^1;
if (!insta[v]) {sta[++top]=v; insta[v]=1;}
q.push(make(-dis[v], v));
}
}
}
puts("error");
}
void solve() {
memset(dis, 0x3f, sizeof(dis));
for (int i=1; i<=n; ++i) if (!col[i]) dij(i);
for (int i=2; i<=m*2; i+=2) if (keep[i]) ans+=e[i].val;
printf("%lld\n", ans);
exit(0);
}
}
signed main()
{
freopen("minimum.in", "r", stdin);
freopen("minimum.out", "w", stdout);
memset(head, -1, sizeof(head));
n=read(); m=read();
for (int i=1; i<=n; ++i) col[i]=read(), fa[i]=i, able[i]=(col[i]==1)?1:0;
for (int i=1,x,y,z; i<=m; ++i) {
x=read(); y=read(); z=read();
add(x, y, z); add(y, x, z);
int f1=find(x), f2=find(y);
if (f1!=f2) {fa[f1]=f2; able[f2]|=able[f1];}
}
for (int i=1; i<=n; ++i) if (!col[i] && !able[find(i)]) {puts("impossible"); return 0;}
task1::solve();
return 0;
}