| ||||||||||
| 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 | |||||||||
我真的找不出这俩个程序之间到底有什么 差别了 为什么一个超时一个却79ms 麻烦哪位牛牛帮忙分析一下 谢谢rt
这个是超时的:
#include<iostream>
#include<queue>
#include<cstdlib>
#include<cstdio>
#define INF 1000000
using namespace std;
typedef struct Stat
{
int len;
int x;
int cost;
friend bool operator<(Stat a,Stat b)
{
return a.len>b.len;
}
}Stat;
int cost[105][105]={0};
int map[105][105]={0};
int Graph[105][105]={0};
int n,m,k;
Stat start,t,T;
int dist[105];
priority_queue<Stat>Queue;
int main()
{
int x,y,dis,pay,i,j;
start.x=1;
start.len=0;
start.cost=0;
scanf("%d %d %d",&n,&m,&k);
dist[m]=INF;
while(k--)
{
scanf("%d %d %d %d",&x,&y,&dis,&pay);
map[x][++map[x][0]]=y;
Graph[x][y]=dis;
cost[x][y]=pay;
}
Queue.push(start);
while(!Queue.empty())
{
t=Queue.top();
Queue.pop();
dist[t.x]=t.len;
if(t.x==m)
break;
for(i=1;i<=map[t.x][0];i++)
{
if(cost[t.x][map[t.x][i]]+t.cost<=n)
{
T.len=t.len+Graph[t.x][map[t.x][i]];
T.x=map[t.x][i];
T.cost=cost[t.x][map[t.x][i]]+t.cost;
Queue.push(T);
}
}
}
if(dist[m]>=INF)
printf("-1\n");
else
printf("%d\n",dist[m]);
return 0;
}
这个是AC 79ms的
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define inf 10000000
int N,K,R,dis[105];
struct Road
{
int d,l,t;
}r;
struct P
{
int v,dist,val;
friend bool operator<(P a,P b){
return a.dist > b.dist;
}
};
vector<Road> v[105];
priority_queue<P> q;
int main()
{
int i,j,s,d,l,t;
while(scanf("%d%d%d",&K,&N,&R)==3)
{
for(i=1;i<=N;i++)
{
v[i].clear();
dis[i]=inf;
}
for(i=0;i<R;i++)
{
scanf("%d%d%d%d",&s,&d,&l,&t);
r.d=d;r.l=l;r.t=t;
v[s].push_back(r);
}
P pp,qq;
while(!q.empty()) q.pop();
pp.v=1; pp.dist=0;pp.val=0;
q.push(pp);
while(!q.empty())
{
pp=q.top(); q.pop();
s=pp.v;
dis[s]=pp.dist;
if(s==N)
break;
for(i=0;i<v[s].size();i++)
{
r=v[s][i];
if(pp.val+r.t<=K)
{
qq.v=r.d;
qq.dist=pp.dist+r.l;
qq.val=pp.val+r.t;
q.push(qq);
}
}
}
if(dis[N]!=inf)
printf("%d\n",dis[N]);
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