# 【luogu P3953 逛公园】 题解

$if(dis[v] == dis[u] + e[i].len) ans[e[i].to] = (ans[e[i].to] + ans[now]);$

$if(dis[e[i].to] > dis[now] + e[i].len) ans[e[i].to] = ans[now];$

20分代码如下：

（SPFA又暴毙了？）

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200010;
int dis[maxn], ans[maxn], n, m, k, T, p;
bool vis[maxn];
struct edge{
int next, to, len;
}e[maxn<<2];
int head[maxn], cnt;
queue<int> q;
void add(int u, int v, int w)
{
e[++cnt].len = w;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
int Ans;
void SPFA()
{
while(!q.empty())
{
int now = q.front(); q.pop();
vis[now] = 0;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(dis[e[i].to] == dis[now] + e[i].len)
{
ans[e[i].to] = (ans[e[i].to] + ans[now]);
if(!vis[e[i].to])
{
q.push(e[i].to);
vis[e[i].to]=true;
}
}
if(dis[e[i].to] > dis[now] + e[i].len)
{
dis[e[i].to] = dis[now] + e[i].len;
ans[e[i].to] = ans[now];
if(!vis[e[i].to] /*&& ans[e[i].to]*/)
{
q.push(e[i].to);
vis[e[i].to] = 1;
}
}
}
if(now==n)	Ans+=ans[now];
ans[now]=0;
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(head, -1, sizeof(head));
memset(dis, 127, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(ans, 0, sizeof(ans));
while(!q.empty()) q.pop();
scanf("%d%d%d%d",&n,&m,&k,&p);
for(int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
add(u, v, w);
}
dis[1] = 0;
vis[1] = 1;
ans[1] = 1;
q.push(1);
SPFA();
printf("%d\n",Ans);
Ans=0;
}
}


$f[u][j] = Σf[v][dis[u]+j-w-dis[v]]$

u是当前点，v是它的前驱，w是它与它前驱的边的权值，j是相差为j，dis[x]表示1—>x的最短路

for(i = 1; i <= k; i++)
ans += f[n][i];

code：

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200010;
int dis[maxn], ans, n, m, mod, k, flag, T, f[maxn][51], used[maxn][51];
bool vis[maxn];
struct edge{
int to, next, len;
}e1[maxn<<2], e2[maxn<<2];//正图1   反图2
int head1[maxn], head2[maxn], cnt1, cnt2;
void add1(int u, int v, int w)
{
e1[++cnt1].len = w;
e1[cnt1].next = head1[u];
e1[cnt1].to = v;
head1[u] = cnt1;
}
void add2(int u, int v, int w)
{
e2[++cnt2].len = w;
e2[cnt2].next = head2[u];
e2[cnt2].to = v;
head2[u] = cnt2;
}
//----------------------------------
void SPFA()
{
queue<int> q;
memset(dis, 64, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[1] = 0, vis[1] = 1;
q.push(1);
while(!q.empty())
{
int now = q.front(); q.pop();
vis[now] = 0;
for(int i = head1[now]; i != -1; i = e1[i].next)
{
if(dis[e1[i].to] > dis[now] + e1[i].len)
{
dis[e1[i].to] = dis[now] + e1[i].len;
if(!vis[e1[i].to])
{
q.push(e1[i].to);
vis[e1[i].to] = 1;
}
}
}
}
}
//----------------------------------
int dfs(int u, int k)
{
if(~f[u][k]) return f[u][k];
used[u][k] = 1;
f[u][k] = 0;
for(int i = head2[u]; i != -1; i = e2[i].next)
{
int v = e2[i].to, w = e2[i].len;
int t = dis[u] + k - w - dis[v];
if(t < 0) continue;
if(used[v][t]) flag = 1;
f[u][k] = (f[u][k] + dfs(v, t)) % mod;
}
used[u][k] = 0;
return f[u][k];
}
//----------------------------------
void init()
{
memset(f, -1, sizeof(f));
memset(used, 0, sizeof(used));
memset(e1, 0, sizeof(e1));
memset(e2, 0, sizeof(e2));
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
cnt1 = 0, cnt2 = 0, ans = 0, flag = 0;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&k,&mod);
init();
for(int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d",&u, &v, &w);
add1(u, v, w);
add2(v, u, w);
}
SPFA();
f[1][0] = 1;
for(int i = 0; i <= k; i++)
ans = (ans + dfs(n, i))%mod;
dfs(n, k+1);
if(flag) {printf("-1\n"); continue;}
printf("%d\n",ans);
}
return 0;
}


（SPFA没有死！）

posted @ 2018-10-19 21:27  Misaka_Azusa  阅读(74)  评论(0编辑  收藏
Live2D