话说这道题昨天就应该ac的,悲剧的是,昨天电脑出了点问题,,耽误了,,,今天重新写了下代码后,ac了。这道题就是用优先队列,图用邻接表存,再用spfa(),时间会减少很多。题目:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7021 | Accepted: 2623 |
Description
Input
Output
Sample Input
5 6 7 1 2 2 3 2 4 3 3 3 4 2 4 1 3 4 1 4 6 2 1 3 5 2 0 5 4 3 2
Sample Output
11ac代码:
#include
#include
#include
#include
using namespace std;
const int N=110;
const int M=10010;
struct edge{
int rp,value,cost,next;
}ee[M];
struct point{
int sp,len,fee;
bool operator<(const struct point a)const{
if(a.len==len)
return a.fee qq;
int totalmoney,numcity,numpath,num,head[M],dis[N];
void addedge(int x,int y,int ll,int v){
ee[num].rp=y;
ee[num].value=ll;
ee[num].cost=v;
ee[num].next=head[x];
head[x]=num++;
}
void spfa(){
while(!qq.empty())
qq.pop();
p1.fee=0;p1.len=0;p1.sp=1;
qq.push(p1);
while(!qq.empty()){
p2=qq.top();
qq.pop();
int x=p2.sp;
if(x==numcity){
printf("%d\n",p2.len);
return;
}
for(int i=head[x];i!=-1;i=ee[i].next){
int y=ee[i].rp;
if(p2.fee+ee[i].cost<=totalmoney){
p1.fee=p2.fee+ee[i].cost;
p1.len=p2.len+ee[i].value;
p1.sp=y;
qq.push(p1);
}
}
}
printf("-1\n");
return ;
}
int main(){
//freopen("1.txt","r",stdin);
while(~scanf("%d",&totalmoney)){
num=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&numcity,&numpath);
int x,y,ll,v;
for(int i=1;i<=numpath;++i){
scanf("%d%d%d%d",&x,&y,&ll,&v);
addedge(x,y,ll,v);
}
spfa();
}
return 0;
}