zoj3408(最短路+dp)

C - Gao
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

Who is cpcs? Every one in ZJU ACM-ICPC team knows him. Although he has been graduated from ZJU, he is still active in ZOJ and many contests. cpcs is the legendary figure in ZJU ACM-ICPC history. It is not only because he has passed nearly all the problems on ZOJ, but also because his very famous function name "gao" which is now widely spreaded and used by many ACMers.

"gao" in Chinese means to do something but it's not that formal in grammar. So it is usually used verbally. In addition, "gao" can be explained as a spirit, a belief, even an attitude to life, which makes cpcs use "gao" as most of the function names (Of course there should be some functions named such as "main" in C or C++).

One day, cpcs got a map of the city he lived in. There were N buildings (numbered from 0 to N - 1) in the map with the building he was in marked as 0. Between the buildings, there were some unidirectional roads. He then wrote a program with function "gao" that could calculate all the shortest paths from building 0 to all the building. Here, there might be more than one shortest paths to a building. Now he wanted to know, for each query of buildings, how many shortest paths that he figured out with function "gao" passed through the queried building. Note that if a building is unreachable from building 0, no shortest path would passes through it.

Input

There are multiple test cases. For each case, the first line is three numbers N, M, Q (1 ≤ Q ≤ N ≤ 10000, 1 ≤ M ≤ 50000) indicating the number of buildings, the number of roads and the number of queries. Next is M lines each containing three numbers a, b, d (0 ≤ a, b < N, 0 < d ≤ 200) indicating that there is a road of length d from building a to building b. Next is Q lines each containing one integer qiindicating a query to a building. Most cases are small, no more than 5 large cases.

Output

In each case, for each query, you should print one line with the number of shortest paths that pass through the building. Since the answer may be very large, you only need to print the last 10 digits of the answer.

Sample Input

4 4 4
0 1 2
0 2 2
1 3 1
2 3 1
0
1
2
3

Sample Output

0000000005
0000000002
0000000002
0000000002
Hint

In the sample, there are 5 shortest paths.

  • 0
  • 0 -> 1
  • 0 -> 2
  • 0 -> 1 -> 3
  • 0 -> 2 -> 3
Therefore, building 0 appears 5 times in the shortest paths, while building 1, 2 and 3 each appears 2, 2, 2 times.

题意:找出每个顶点被多少条最短路(起点为0)经过

思路:先用spfa跑出从0点到其它顶点的最短路,然后去掉不在最短路上的边构成一幅新图,然后在新图上从0点跑一边正向的dfs可以求出以各个顶点为起点的最短路数量

anss[i],然后反向跑一遍dfs可以求出以各个顶点为终点的最短路数量anse[i],然后答案就是anss[i]*anse[i],最后乘的时候可能会溢出long long,所以可以用布斯乘法防溢出

#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
        
          using namespace std; #define INF ((1<<31)-1) typedef long long ll; const ll mod = 10000000000; int n,m,Q; int head[10010]; int edgeu[50010]; int edge[50010]; int next[50010]; int vis[10010]; int ww[50010]; int dp[10010]; int fa[10010]; int d; queue 
         
           q; int head1[10010]; int edge1[50010]; int next1[50010]; int head2[10010]; int edge2[50010]; int next2[50010]; ll anss[10010]; ll anse[10010]; int ddd; void add(int u,int v,int w) { edgeu[d]=u; edge[d]=v; ww[d]=w; next[d]=head[u]; head[u]=d++; } void add1(int u,int v) { edge1[d]=v; next1[d]=head1[u]; head1[u]=d++; } void add2(int u,int v) { edge2[ddd]=v; next2[ddd]=head2[u]; head2[u]=ddd++; } void spfa() { int i; memset(vis,0,sizeof(vis)); for(i=0;i 
          
            (temp=dp[f]+ww[i])) { dp[edge[i]]=temp; if(!vis[edge[i]]) { vis[edge[i]]=1; q.push(edge[i]); } } } } void dfs(int u) { int i; if(anss[u])return; if(head1[u]==-1){ anss[u]=1; return; } for(i=head1[u];i!=-1;i=next1[i]){ dfs(edge1[i]); anss[u]=(anss[u]+anss[edge1[i]])%mod; } anss[u]=(anss[u]+1)%mod; } void dfs1(int u) { int i; if(anse[u])return; if(head2[u]==-1){ anse[u]=1; return; } for(i=head2[u];i!=-1;i=next2[i]){ dfs1(edge2[i]); anse[u]=(anse[u]+anse[edge2[i]])%mod; } } long long mul(long long a,long long b) { long long la = a % 100000; long long lb = b % 100000; return ( (a / 100000 * lb + b / 100000 * la) * 100000 + la * lb) % mod; } int main() { while(scanf("%d%d%d",&n,&m,&Q)!=EOF) { int i,j,u,v,w; d=0; memset(head,-1,sizeof(head)); memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); memset(anss,0,sizeof(anss)); memset(anse,0,sizeof(anse)); for(i=0;i 
            
           
          
        
       
       
      
      
     
     
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值