Full Tank? UVA - 11367
2017-04-22 12:32
330 查看
原来最短路不难。自己老是觉得自己不会。。
虽然这道题我是看了别人的博客才写出来嘚。。
没想到这里真的就是一升一升地加。。居然不会TLE。。,应该是在这个地方用djk比较快。。
不过还是觉得自己敲的比较慢。。。
看了http://blog.csdn.net/murmured/article/details/18847005
虽然这道题我是看了别人的博客才写出来嘚。。
没想到这里真的就是一升一升地加。。居然不会TLE。。,应该是在这个地方用djk比较快。。
不过还是觉得自己敲的比较慢。。。
看了http://blog.csdn.net/murmured/article/details/18847005
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<string> #include<cstring> #include<iomanip> #include<iostream> #include<stack> #include<cmath> #include<map> #include<vector> #define ll long long #define inf 0x3f3f3f3f #define INF 1e9 #define bug1 cout<<"bug1"<<endl; #define bug2 cout<<"bug2"<<endl; #define bug3 cout<<"bug3"<<endl; using namespace std; #define sf scanf #define pf printf #define mem(a,b) memset(a,b,sizeof(a)); const int maxn=1005; int n,m; int d[maxn][105]; int vis[maxn][105]; int p[maxn]; struct Edge{ int v,nxt,c; }edge[maxn*20]; int tol; int head[maxn]; void addedge(int u,int v,int c){ edge[tol].c=c;edge[tol].v=v; edge[tol].nxt=head[u]; head[u]=tol++; } struct Node{ int u; int w; int cost; bool friend operator<(Node a,Node b){ return a.cost>b.cost; } }; void spfa(int s,int e,int num){ priority_queue<Node>q; mem(d,inf); mem(vis,0); Node ss;ss.u=s;ss.w=0;ss.cost=0; d[s][0]=0; q.push(ss); while(!q.empty()){ Node top=q.top();q.pop(); int u=top.u;int cost=top.cost;int w=top.w; vis[u][w]=1; if(u==e){ pf("%d\n",cost);return; } if(w<num&&d[u][w+1]>d[u][w]+p[u]){ d[u][w+1]=d[u][w]+p[u]; Node nv=top;nv.cost=d[u][w+1];nv.w++; q.push(nv); } for(int i=head[u];~i;i=edge[i].nxt){ int v=edge[i].v; if(!vis[v][top.w-edge[i].c]&&top.w>=edge[i].c&&d[v][top.w-edge[i].c]>d[u][top.w]){ d[v][top.w-edge[i].c]=d[u][top.w]; Node nv=top;nv.u=v;nv.w=top.w-edge[i].c; q.push(nv); } } } pf("impossible\n"); } int main(){ while(~sf("%d%d",&n,&m)){ mem(head,-1);tol=0; for(int i=0;i<n;++i){ sf("%d",&p[i]); } for(int i=1;i<=m;++i){ int u,v,c; sf("%d%d%d",&u,&v,&c); addedge(u,v,c); addedge(v,u,c); } int q; sf("%d",&q); for(int i=1;i<=q;++i){ int w,s,e; sf("%d%d%d",&w,&s,&e); spfa(s,e,w); } } }
相关文章推荐
- Full Tank? UVA - 11367
- UVA 11367 - Full Tank?(最短路+DP)
- UVa 11367 Full Tank? (DP + Dijkstra)
- UVA 11367 Full Tank?(bfs最短路)
- UVA11367 Full Tank? 【分层图最短路】
- UVA 11367 Full Tank? (dij+heap+dp)
- uva11367 - Full Tank? 最短路 DP
- UVA 11367 - Full Tank? dijkstra+DP
- uva 11367 - Full Tank?(dijkstra TLE)
- UVA 11367 Full Tank?(bfs最短路)
- UVA 11367 - Full Tank? dijkstra+DP
- (beginer)最短路 UVA 11367 Full Tank?
- Uva 11367 Full Tank?(多状态最短路)
- uva 11367 - Full Tank?(最短路)
- UVA-11367 - Full Tank? (dijkstra)
- UVA-11367 Full Tank? (dijkstra)
- POJ 3635 Full Tank? (dp)
- POJ 3635 Full Tank? 最短路变形
- poj 3635 Full Tank? 广搜?
- POJ 3635 Full Tank?