题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=301
题意:给出一个无向图,起点s和终点t。一个人从s到t,已知他走的是最短路。问这个人到达点i时,有可能在那些点上?(1<=i<=n)
思路:先从s和tSPFA一次,可以得到所有的关键路径。然后就是枚举每个路径覆盖了哪些点。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <string>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define DOW0(i,x) for(i=x;i>=0;i--)
#define DOW1(i,x) for(i=x;i>=1;i--)
#define DOW(i,a,b) for(i=a;i>=b;i--)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u64 &x){scanf("%llu",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u64 &x,u64 &y){scanf("%llu%llu",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(u64 &x,u64 &y,u64 &z){scanf("%llu%llu%llu",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(double x) {printf("%lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
class node
{
public:
int u,v,dis;
void get()
{
RD(u,v,dis);
}
};
const int INF=1<<29;
const int N=2005;
const int M=200005;
int n,m,s,t,dis[N],dis1[N];
node a[M];
vector<int> G[N],p;
void SPFA(int s,int dis[])
{
queue<int> Q;
int i,j,k,t,d,inQ[N]={0};
FOR1(i,n) dis[i]=INF;
dis[s]=0;
Q.push(s);
while(!Q.empty())
{
k=Q.front();
Q.pop();
inQ[k]=0;
FOR0(i,SZ(G[k]))
{
j=G[k][i];
t=a[j].u+a[j].v-k;
d=a[j].dis;
if(dis[t]>dis[k]+d)
{
dis[t]=dis[k]+d;
if(!inQ[t]) inQ[t]=1,Q.push(t);
}
}
}
}
int cmp(int x,int y)
{
return dis[x]<dis[y];
}
vector<pair<int,int> > V;
int main()
{
RD(n,s,t);
RD(m);
int i,j,k,u,v,d;
FOR1(i,m) a[i].get(),G[a[i].u].pb(i),G[a[i].v].pb(i);
SPFA(s,dis);
SPFA(t,dis1);
int minDis=dis[t];
FOR1(i,n) if(dis[i]+dis1[i]==minDis) p.pb(i);
sort(p.begin(),p.end(),cmp);
FOR1(i,m)
{
u=a[i].u;
v=a[i].v;
d=a[i].dis;
if(dis[u]>dis[v]) swap(u,v);
if(dis[u]+d==dis[v]&&dis[v]+dis1[v]==minDis)
{
V.pb(MP(dis[u]+1,1));
V.pb(MP(dis[v],-1));
}
}
sort(V.begin(),V.end());
int ans[N]={0},cur=0,sum=0;
for(i=0;i<SZ(p);)
{
while(cur<SZ(V)&&V[cur].first<=dis[p[i]])
{
sum+=V[cur++].second;
}
for(j=i;j<SZ(p)&&dis[p[j]]==dis[p[i]];j++)
{
ans[p[i]]++;
}
ans[p[i]]+=sum;
for(k=i;k<j;k++) ans[p[k]]=ans[p[i]];
i=j;
}
FOR1(i,n) printf("%d ",ans[i]);puts("");
return 0;
}