比赛 |
9.27练习赛 |
评测结果 |
AAAAAAAAAAAT |
题目名称 |
观光 |
最终得分 |
92 |
用户昵称 |
袁书杰 |
运行时间 |
3.159 s |
代码语言 |
C++ |
内存使用 |
51.10 MiB |
提交时间 |
2024-09-27 21:39:01 |
显示代码纯文本
- #include<bits/stdc++.h>
- #define int long long
- using namespace std;
- struct edge{
- int u,v,w,nxt;
- }e[1000005];
- int etot,head[1000005],dis[1000005],now,ans;
- bool vis[1000005],flag[1000005];
- void adde(int u,int v,int w){
- e[++etot]={u,v,w,head[u]};
- head[u]=etot;
- }
- struct node{
- int u,dis;
- bool operator<(const node&a)const{
- return a.dis<dis;
- }
- };
- priority_queue<node> q;
- void dij(int s){
- for(int i=0; i<=1000000; i++) {
- dis[i]=5e18;
- }
- q.push(node{s,0});
- dis[s]=0;
- while(!q.empty()){
- int u=q.top().u;
- q.pop();
- if(vis[u]){
- continue;
- }
- vis[u]=1;
- for(int i=head[u];i;i=e[i].nxt){
- int v=e[i].v,w=e[i].w;
- if(dis[v]>dis[u]+w){
- dis[v]=dis[u]+w;
- q.push(node{v,dis[v]});
- }
- }
- }
- }
- int n,m,s,t;
- void dfs(int u,int father,int len){
- if(u==t){
- if(len==now||len==now-1){
- ans++;
- return;
- }
- }
- if(len>now){
- return;
- }
- for(int i=head[u];i;i=e[i].nxt){
- int v=e[i].v;
- if(!flag[v]){
- flag[u]=true;
- dfs(v,u,len+e[i].w);
- flag[u]=false;
- }
- }
- }
- signed main(){
- freopen("sightseeing.in","r",stdin);
- freopen("sightseeing.out","w",stdout);
- ios::sync_with_stdio(false);
- cin.tie(0),cout.tie(0);
- int T;
- cin>>T;
- while(T--){
- cin>>n>>m;
- memset(vis,0,sizeof(vis));
- memset(flag,0,sizeof(flag));
- memset(head,0,sizeof(head));
- etot=0;
- now=0;
- ans=0;
- memset(e,0,sizeof(e));
- for(int i=1;i<=m;i++){
- int u,v,w;
- cin>>u>>v>>w;
- adde(u,v,w);
- }
- cin>>s>>t;
- dij(s);
- now=dis[t]+1;
- dfs(s,0,0);
- cout<<ans<<'\n';
- }
- return 0;
- }