<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">被各种坑坑到意识模糊</span>
<pre name="code" class="cpp">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
#define U G[p].u
#define V G[p].v
namespace DINIC{
const int N=405;
const int M=120005;
struct edge{
int u,v,f;
int next;
}G[M];
int head[N],inum=1;
inline void add(int u,int v,int f,int p){
G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
}
inline void link(int u,int v,int f){
add(u,v,f,++inum); add(v,u,0,++inum);
}
int S,T;
int dis[N];
int Q[N],l,r;
inline bool bfs(){
memset(dis,-1,sizeof(dis)); l=r=-1;
Q[++r]=S; dis[S]=1;
while (l<r){
int u=Q[++l];
for (int p=head[u];p;p=G[p].next)
if (G[p].f && dis[V]==-1){
dis[V]=dis[u]+1; Q[++r]=V;
if (V==T) return 1;
}
}
return 0;
}
int cur[N];
ll dfs(int u,ll flow){
if (u==T) return flow;
ll used=0,now;
for (int p=cur[u];p;p=G[p].next)
{
cur[u]=p;
if (G[p].f && dis[V]==dis[u]+1)
{
now=dfs(V,min(flow-used,(ll)G[p].f));
G[p].f-=now,G[p^1].f+=now;
used+=now; if (flow==used) break;
}
}
if (!used) dis[u]=-1;
return used;
}
inline ll Dinic(){
ll ret=0;
while (bfs())
memcpy(cur,head,sizeof(cur)),ret+=dfs(S,1LL<<60);
return ret;
}
inline void clear(){
inum=1; cl(head);
}
}
int n,m;
int A[405];
namespace SPFA{
const int N=405;
const int M=8005;
struct edge{
int u,v,w,next;
}G[M];
int head[N],inum;
inline void add(int u,int v,int w,int p){
G[p].u=u; G[p].v=v; G[p].w=w; G[p].next=head[u]; head[u]=p;
}
inline void link(int u,int v,int w){
add(u,v,w,++inum); add(v,u,w,++inum);
}
long long dist[N],ins[N];
const int NQ=100005;
int Q[NQ],l,r;
inline void Spfa()
{
int u; memset(dist,0x3f,sizeof(dist)); cl(ins);
dist[1]=0; l=r=-1; Q[(++r)%NQ]=1; ins[1]=1;
while (l!=r)
{
u=Q[(++l)%NQ]; ins[u]=0;
for (int p=head[u];p;p=G[p].next)
if (dist[V]>dist[u]+G[p].w)
{
dist[V]=dist[u]+G[p].w;
if (!ins[V]) Q[(++r)%NQ]=V,ins[V]=1;
}
}
}
inline void clear(){
inum=0; cl(head);
}
}
const int N=405;
int clk,pre[N],low[N];
int scc[N],cnt;
int Stk[N],pnt;
inline void Tarjan(int u)
{
using namespace DINIC;
pre[u]=low[u]=++clk; Stk[++pnt]=u;
for (int p=head[u];p;p=G[p].next)
{
if (!G[p].f) continue;
if (!pre[V])
{
Tarjan(V);
low[u]=min(low[u],low[V]);
}
else if (!scc[V])
low[u]=min(low[u],pre[V]);
}
if (low[u]==pre[u])
{
int v; ++cnt;
for (int v;v!=u;)
v=Stk[pnt--],scc[v]=cnt;
}
}
int ans;
int vst[N];
inline void Link(int u,int fa=0)
{
using namespace SPFA;
if (vst[u]) return;
vst[u]=1;
for (int p=head[u];p;p=G[p].next)
if (dist[u]==dist[V]+G[p].w)
DINIC::link(V,u,min(A[V],A[u])),Link(V,u);
}
inline void Build(){
cl(vst); Link(n);
DINIC::S=1; DINIC::T=n; ans=DINIC::Dinic();
}
int main()
{
int iu,iv,iw; int T;
read(T);
while (T--)
{
read(n); read(m);
for (int i=1;i<n;i++) read(A[i]); A[n]=1<<30;
for (int i=1;i<=m;i++)
read(iu),read(iv),read(iw),SPFA::link(iu,iv,iw);
SPFA::Spfa();
Build();
pnt=cnt=0; clk=0; cl(pre); cl(low); cl(scc);
for (int i=1;i<=n;i++)
if (!pre[i])
Tarjan(i);
int flag=0;
using namespace DINIC;
for (int p=2;p<=inum;p+=2)
{
if (G[p].f) continue;
if (scc[U]!=scc[V] && !(scc[U]==scc[1] && scc[V]==scc[n]))
flag=1;
if (scc[U]==scc[1] && scc[V]==scc[n] && A[U]==A[V])
flag=1;
}
!flag?printf("Yes"):printf("No");
printf(" %d\n",ans);
DINIC::clear(); SPFA::clear();
}
return 0;
}