hdu2145 邻接表spfa

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<queue>
 5 using namespace std;
 6 struct Edge
 7 {
 8     int to;  //边终点
 9     int w;   //边值
10     int next;//结构体模拟链表
11 }edge[50005];
12 int m,n,target,outqueue[305],used[305],head[305],dis[305],start[305],speed[305];
13 int spfa(int sta)
14 {
15     int temp,i;
16     queue<int>q;
17     while (!q.empty()) q.pop();
18     used[sta]=1; dis[sta]=0; q.push(sta);
19     while (!q.empty())
20     {
21         temp=q.front(); q.pop();
22         outqueue[temp]++; used[temp]=0;
23         if (outqueue[temp]>n) return -1;
24         for (i=head[temp];i!=-1;i=edge[i].next)
25         if (dis[edge[i].to]>dis[temp]+edge[i].w)
26         {
27             dis[edge[i].to]=dis[temp]+edge[i].w;
28             if (used[edge[i].to]==0)
29             {
30                 used[edge[i].to]=1;
31                 q.push(edge[i].to);
32             }
33         }
34     }
35     return 1;
36 }
37 int main()
38 {
39     int i,k,a,b,w,judge,maxdis,x;
40     double min;
41     while (~scanf("%d%d%d",&n,&m,&k))
42     {
43         memset(outqueue,0,sizeof(outqueue));//出队次数判断负权回路
44         memset(used,0,sizeof(used));//用不用加入队列
45         memset(head,-1,sizeof(head));//数组模拟链表
46         for (i=1;i<=n;i++) dis[i]=10000000;
47         for (i=1;i<=k;i++)
48         {
49             scanf("%d%d%d",&b,&a,&w);
50             edge[i].to=b;
51             edge[i].w=w;
52             edge[i].next=head[a];
53             head[a]=i;
54         }
55         scanf("%d",&target);
56         for (i=1;i<=m;i++) scanf("%d",&start[i]);
57         for (i=1;i<=m;i++) scanf("%d",&speed[i]);
58         min=10000000.0; maxdis=0;
59         judge=spfa(target);
60         for (i=1;i<=m;i++)
61         {
62             x=dis[start[i]];
63             if (x!=10000000&&(1.0*x/speed[i]<min||(fabs(1.0*x/speed[i]-min)<1e-6&&x>=maxdis)))
64             { min=1.0*x/speed[i]; maxdis=x; judge=i; }
65         }
66         if (fabs(min-10000000.0)<1e-6) printf("No one\n");
67         else printf("%d\n",judge);
68     }
69 }

http://acm.hdu.edu.cn/showproblem.php?pid=2145

posted on 2014-07-17 17:22  xiao_xin  阅读(98)  评论(0编辑  收藏  举报

导航