上交三月月赛[SJTU] 1105 path

这题主要是字符读入的问题吧,对于NAME的读入,一直getchar()到',',对于后面的五个属性from,to,length,people number,light只读数字就可以了,而且他们的处理相同,可以用一个函数来实现
下面是代码:
1.                       #include<iostream>
2.                       #include<cstdio>
3.                       #include<cstring>
4.                       #include<queue>
5.                       #include<algorithm>
6.                       using namespace std;
7.                       struct Edge{
8.                           long a,b;
9.                       };
10.                    struct Edge e[30002];
11.                    long pos[1001],next[30002],d[1001];
12.                    long w[30002][3];
13.                    bool v[1001];
14.                    long n,m,s,t,tt,tot;
15.                     
16.                    long init(void)
17.                    {
18.                        char c;
19.                        long k=0;
20.                        c=getchar();
21.                        while (!((c>='0')&&(c<='9'))){c=getchar();}
22.                        while ((c>='0')&&(c<='9')){
23.                            k=k*10+c-'0';
24.                            c=getchar();
25.                        }
26.                        return k;
27.                    }
28.                     
29.                    void build(long x,long y,long len,long pn,long light)
30.                    {
31.                        tot++;
32.                        e[tot].a=x;e[tot].b=y;w[tot][0]=len;w[tot][1]=pn;w[tot][2]=light;
33.                        next[tot]=pos[x];
34.                        pos[x]=tot;
35.                    }
36.                     
37.                    void spfa(long s,long t,long QU)
38.                    {
39.                        queue<long> q;
40.                        memset(v,sizeof(v),0);
41.                        for (long i=1;i<=n;i++)d[i]=1000000000;
42.                        d[s]=0;v[s]=true;q.push(s);
43.                        while (!q.empty())
44.                        {
45.                            long now=q.front();q.pop();
46.                            long k=pos[now];
47.                            while (k)
48.                            {
49.                                long y=e[k].b;
50.                                if (d[y]>d[now]+w[k][QU])
51.                                {
52.                                    d[y]=d[now]+w[k][QU];
53.                                    if (!v[y])
54.                                    {
55.                                        q.push(y);v[y]=true;
56.                                    }
57.                     
58.                                }
59.                                k=next[k];
60.                            }
61.                            v[now]=false;
62.                        }
63.                        printf("%ld",d[t]);
64.                        if (QU!=2)cout<<' '; else cout<<endl;
65.                     
66.                    }
67.                     
68.                    int main()
69.                    {
70.                     
71.                     
72.                      cin>>tt;
73.                        while(tt--){
74.                            cin>>n>>m>>s>>t;
75.                            tot=0;
76.                            memset(pos,0,sizeof(pos));
77.                            for (long i=0;i<m;i++)
78.                            {
79.                                char s;
80.                                s=getchar(); while (s!=',') s=getchar();
81.                                long x=init();//cout<<x<<endl;
82.                                long y=init();//cout<<y<<endl;
83.                                long len=init();//cout<<len<<endl;
84.                                long pn=init();//cout<<pn<<endl;
85.                                long light=init();//cout<<light<<endl;
86.                                build(x,y,len,pn,light);
87.                                build(y,x,len,pn,light);
88.                            }
89.                            spfa(s,t,0);
90.                            spfa(s,t,1);
91.                            spfa(s,t,2);
92.                        }
93.                        return 0;
94.                    }
posted @ 2013-03-11 17:50  ustc-acm  阅读(169)  评论(0编辑  收藏  举报