1,acwing 904 虫洞;2,acwing 1140.最短网络;
1,虫洞;
简单的判断负环,注意根据题目建图,理解实际问题中的图;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long LL;
typedef double dob;
const int N=1e4+10;
int n,m1,m2;
int h[N],e[N],ne[N],w[N],idx;
int dist[N],cnt[N],st[N];
void add(int a,int b,int c)
{
w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
bool spfa()
{
memset(dist,0,dist);
memset(cnt,0,cnt);
memset(st,0,st);
queue<int>q;
rep2(i,1,n)
{
q.push(i);
st[i]=1;
}
while(q.size())
{
int t=q.front();q.pop();
st[t]=0;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=n)return 1;
if(!st[j])
{
q.push(j);
st[j]=1;
}
}
}
}
return 0;
}
void solve()
{
cin>>n>>m1>>m2;
memset(h,-1,h);
idx=0;
rep1(i,0,m1)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
rep1(i,0,m2)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,-c);
}
if(spfa())YES;
else NO;
}
signed main()
{
quick_cin();
int T;
cin>>T;
while(T--)solve();
return 0;
}
2,最短网络;
最小生成树模板题;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
using namespace std;
typedef pair<long long,long long>PLL;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<int,PII> PIII;
typedef double dob;
const int N=110;
int a[N][N];
struct node
{
int a,b,w;
}edge[N*N];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int p[N*N];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int n;
int cnt;
void kruskal()
{
rep2(i,1,n)p[i]=i;
sort(edge,edge+cnt,cmp);
int gs=0;
int ans=0;
rep1(i,0,cnt)
{
int a=edge[i].a,b=edge[i].b,w=edge[i].w;
a=find(a);
b=find(b);
if(a!=b)
{
p[a]=b;
gs++;
if(gs==n)break;
ans+=w;
}
}
cout<<ans;
}
signed main()
{
quick_cin();
cin>>n;
rep2(i,1,n)
rep2(j,1,n)
cin>>a[i][j];
rep2(i,2,n)
rep2(j,1,i-1)
{
edge[cnt++]={i,j,a[i][j]};
}
kruskal();
return 0;
}