|
沙发
楼主 |
发表于 2018-6-5 20:08:07
|
只看该作者
- # include<algorithm>
- # include<iostream>
- # include<cstring>
- # include<cstdio>
- # include<queue>
- const int mn = 100010;
- const int maxn = 200010;
- inline int read()
- {
- int x=0;
- char ch=getchar();
- while(ch<'0'||ch>'9') ch=getchar();
- while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
- return x;
- }
- using namespace std;
- int t,n,m,K,p,ans,edge_max;
- int dp[mn][51],mark[mn][51];
- int dis[mn],head1[mn],head2[mn];
- bool flag;
- bool vis[mn];
- queue<int> q;
- struct edge{int to,dis,next;}e1[maxn],e2[maxn];
- void add(int x,int y,int z)
- {
- e1[++edge_max].to=y;
- e1[edge_max].dis=z;
- e1[edge_max].next=head1[x];
- head1[x]=edge_max;
- e2[edge_max].to=x;
- e2[edge_max].dis=z;
- e2[edge_max].next=head2[y];
- head2[y]=edge_max;
- }
- void pre()
- {
- dis[1]=0;
- q.push(1);
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- vis[u]=0;
- for(int i=head1[u];i;i=e1[i].next)
- {
- if(dis[u]+e1[i].dis<dis[e1[i].to])
- {
- dis[e1[i].to]=dis[u]+e1[i].dis;
- if(!vis[e1[i].to])
- {
- vis[e1[i].to]=1;
- q.push(e1[i].to);
- }
- }
- }
- }
- }
- int dfs(int x,int k)
- {
- if(flag)
- return 0;
- if(mark[x][k]==1)//如果有0环,路径则有无数条
- {
- flag=true;
- return 0;
- }
- if(mark[x][k]==2)
- return dp[x][k];
- mark[x][k]=1;
- for(int i=head2[x];i;i=e2[i].next)
- {
- int u=e2[i].to,len=e2[i].dis;
- if(dis[u]+len>dis[x]+k)
- continue;
- dp[x][k]+=dfs(u,k-(dis[u]+len-dis[x]));
- if(flag) return 0;
- dp[x][k]%=p;
- }
- mark[x][k]=2;
- return dp[x][k];
- }
- int main()
- {
- int x,y,z;
- t=read();
- while(t--)
- {
- ans=0;
- flag=false;
- memset(dis,0xf,sizeof(dis));
- memset(head1,0,sizeof(head1));
- memset(head2,0,sizeof(head2));
- memset(dp,0,sizeof(dp));
- memset(mark,0,sizeof(mark));
- edge_max=0;
- n=read(),m=read(),K=read(),p=read();
- for(int i=1;i<=m;i++)
- {
- x=read(),y=read(),z=read();
- add(x,y,z);
- }
- pre();
- dp[1][0]=1;
- for(int i=0;i<=K;i++)
- {
- ans+=dfs(n,i);
- //printf("%d\n",ans);
- ans%=p;
- }
- if(flag)
- puts("-1");
- else printf("%d\n",ans);
- }
- return 0;
- }
复制代码 |
|