比赛 |
9.27练习赛 |
评测结果 |
WWWTWWWTWWTT |
题目名称 |
观光 |
最终得分 |
0 |
用户昵称 |
djyqjy |
运行时间 |
12.035 s |
代码语言 |
C++ |
内存使用 |
3.67 MiB |
提交时间 |
2024-09-27 21:45:51 |
显示代码纯文本
//b T4 186lines example_out 0 0
//add: b T4 example_out 0 0 too after changing
#include<bits/stdc++.h>
using namespace std;
const int N=1010,M=10010;
int t;
int n,m;
int fir[M],ver[M],edge[M],nxt[M],hd[N];
int ver2[M],edge2[M],nxt2[M],hd2[N];
int jsq=1;
int s,f;
void add_edge(int x,int y,int val)
{
fir[++jsq]=x;
ver[jsq]=y;
edge[jsq]=val;
nxt[jsq]=hd[x];
hd[x]=jsq;
ver2[jsq]=x;
edge2[jsq]=val;
nxt2[jsq]=hd2[x];
hd2[x]=jsq;
return;
}
int verr[M],edgee[M],nxtt[M],hdd[N];
int jsqq=1;
void add_edgee(int x,int y,int val)
{
verr[++jsqq]=y;
edgee[jsqq]=val;
nxtt[jsqq]=hdd[x];
hdd[x]=jsqq;
return;
}
int dis1[N],dis2[N];
bool mark[N];
void dij_1()
{
memset(dis1,0x3f,sizeof(dis1));
dis1[s]=0;
priority_queue<pair<int,int> >q;
q.push(make_pair(0,s));
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(mark[x]) continue;
mark[x]=1;
for(int i=hd[x];i;i=nxt[i])
{
int y=ver[i];
if(dis1[y]>dis1[x]+edge[i])
{
dis1[y]=dis1[x]+edge[i];
q.push(make_pair(-dis1[y],y));
}
}
}
return;
}
void dij_2()
{
memset(dis2,0x3f,sizeof(dis2));
dis2[t]=0;
priority_queue<pair<int,int> >q;
q.push(make_pair(0,t));
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(mark[x]) continue;
mark[x]=1;
for(int i=hd2[x];i;i=nxt2[i])
{
int y=ver2[i];
if(dis2[y]>dis2[x]+edge2[i])
{
dis2[y]=dis2[x]+edge2[i];
q.push(make_pair(-dis2[y],y));
}
}
}
return;
}
int rd[N];
struct qz
{
int z,f;
}qs[3][N];
qz qa[7];
bool flag;
bool cmp(qz a,qz b){return a.z<b.z;}
void tp()
{
queue<int> q;
q.push(s);
qs[1][s]=(qz){0,1};
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=hdd[x];i;i=nxtt[i])
{
int y=verr[i];
rd[y]--;
if(rd[y]) continue;
int fi=0;
if(qs[1][x].f) qa[++fi]=(qz){qs[1][x].z+edgee[i],qs[1][x].f};
if(qs[2][x].f) qa[++fi]=(qz){qs[2][x].z+edgee[i],qs[2][x].f};
if(qs[1][y].f)
{
flag=1;
for(int zz=1;zz<=fi;zz++)
if(qa[zz].z==qs[1][y].z)
{
qa[zz].f+=qs[1][y].f;
flag=0;
break;
}
if(flag) qa[++fi]=qs[1][y];
}
if(qs[2][y].f)
{
flag=1;
for(int zz=1;zz<=fi;zz++)
if(qa[zz].z==qs[2][y].z)
{
qa[zz].f+=qs[2][y].f;
flag=0;
break;
}
if(flag) qa[++fi]=qs[2][y];
}
sort(qa+1,qa+1+fi,cmp);
qs[1][y]=qa[1];
if(fi>=2) qs[2][y]=qa[2];
mark[y]=1;
q.push(y);
}
}
return;
}
int main()
{
freopen("sightseeing.in","r",stdin);
freopen("sightseeing.out","w",stdout);
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1,a,b,c;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
scanf("%d%d",&s,&f);
dij_1();
for(int i=1;i<=n;i++) mark[i]=0;
dij_2();
for(int i=2;i<=jsq;i++)
if(dis1[fir[i]]+edge[i]+dis2[ver[i]]<=dis1[f]) add_edgee(fir[i],ver[i],edge[i]),rd[ver[i]]++;
tp();
printf("%d\n",qs[1][f].f+(qs[2][f].z==qs[1][f].z+1?qs[2][f].f:0));
for(int i=2;i<=jsq;i++)
{
fir[i]=0;
ver[i]=0;ver2[i]=0;
edge[i]=0;edge2[i]=0;
nxt[i]=0;nxt2[i]=0;
}
jsq=1;
for(int i=2;i<=jsqq;i++)
{
verr[i]=0;
edgee[i]=0;
nxtt[i]=0;
}
jsqq=1;
for(int i=1;i<=n;i++)
{
hd[i]=0;
hdd[i]=0;
qs[1][i]=(qz){0,0};
qs[2][i]=(qz){0,0};
mark[i]=0;
rd[i]=0;
}
}
return 0;
}