标签:des style blog class code tar
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10202 | Accepted: 3786 |
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
11
//332K 94MS #include<stdio.h> #include<string.h> #define M 107 #define inf 0x3f3f3f int head[M],vis[M],nodes,minlen; int n,m,k; struct E { int u,v,l,c,next; } edge[M*M]; void addedge(int u,int v,int l,int c) { edge[nodes].u=u; edge[nodes].v=v; edge[nodes].l=l; edge[nodes].c=c; edge[nodes].next=head[u]; head[u]=nodes++; } void dfs(int city,int nowlen,int nowmoney) { if(nowlen>minlen)return; if(city==n&&nowlen<minlen&&nowmoney>=0) { minlen=nowlen; return; } for(int i=head[city]; i!=-1; i=edge[i].next) { int v=edge[i].v; int l=edge[i].l; int c=edge[i].c; if(!vis[v]&&nowmoney>=c) { vis[v]=1; dfs(v,nowlen+l,nowmoney-c); vis[v]=0; } } return; } int main() { memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); nodes=0,minlen=inf; scanf("%d%d%d",&k,&n,&m); int u,v,l,c; while(m--) { scanf("%d%d%d%d",&u,&v,&l,&c); addedge(u,v,l,c); } dfs(1,0,k); if(minlen==inf)printf("-1\n"); else printf("%d\n",minlen); }
//1028K 32MS #include<stdio.h> #include<string.h> #include<queue> #define M 1007 using namespace std; int head[M],nodes; int k,n,m; struct Q { int pos,len,money; bool operator<(const Q t)const { return t.len<len; } } ; struct node { int u,v,next,l,c; }edge[M*M]; void addedge(int u,int v,int l,int c) { edge[nodes].u=u; edge[nodes].v=v; edge[nodes].l=l; edge[nodes].c=c; edge[nodes].next=head[u]; head[u]=nodes++; } int bfs(int u) { priority_queue<Q>q; Q now,next; now.pos=1; now.len=0; now.money=0; q.push(now); while(!q.empty()) { now=q.top(); q.pop(); int pos=now.pos,len=now.len,money=now.money; if(pos==n&&money<=k)return len; for(int i=head[pos];i!=-1;i=edge[i].next) { int v=edge[i].v; int l=edge[i].l; int c=edge[i].c; if(money+c<=k) { next.pos=v; next.len=len+l; next.money=money+c; q.push(next); } } } return -1; } int main() { scanf("%d%d%d",&k,&n,&m); nodes=0; memset(head,-1,sizeof(head)); int u,v,l,c; for(int i=0; i<m; i++) { scanf("%d%d%d%d",&u,&v,&l,&c); addedge(u,v,l,c); } int ans=bfs(1); printf("%d\n",ans); }
标签:des style blog class code tar
原文地址:http://blog.csdn.net/crescent__moon/article/details/25224485