Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
WA的可以进来看看这题我WA了好几次,调了快一个小时。 有点心得。 1、先看看自己的程序里有没有输出-1 2、ans应该在第一次访问n的时候就确定 3、具体的自己再看看吧……我已经WA的不想说话了 code: #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<stack> #include<queue> using namespace std; typedef long long LL; const int maxn=110; const int maxm=10010; const int INF=1e9; struct Edge{ int obj,len,cost,next; }e[maxm]; struct Data{ int ord,len,cost; bool operator <(const Data &x) const{ if(x.len==len)return x.cost<cost; return x.len<len; } }; int n,m,K,cur=0,head[maxn]; priority_queue<Data> que; void AddEdge(int a,int b,int c,int d){ cur++; e[cur].obj=b; e[cur].len=c; e[cur].cost=d; e[cur].next=head[a]; head[a]=cur; return; } int main() { freopen("c.in","r",stdin); scanf("%d%d%d",&K,&n,&m); for(int i=1;i<=m;i++){ int u,v,w,p; scanf("%d%d%d%d",&u,&v,&w,&p); AddEdge(u,v,w,p); } Data q; q.cost=0; q.len=0; q.ord=1; que.push(q); int ans=INF; while(!que.empty()){ Data hp=que.top(); que.pop(); int now=hp.ord; if(now==n){ ans=hp.len; break; } for(int h=head[now];h;h=e[h].next){ int son=e[h].obj,used=e[h].cost,l=e[h].len; if(used+hp.cost<=K){ Data p; p.cost=used+hp.cost; p.len=hp.len+l; p.ord=son; que.push(p); } } } if(ans<INF)printf("%d\n",ans); else printf("-1\n"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator