Full Tank? UVA

来源:互联网 发布:德军步兵班 知乎 编辑:程序博客网 时间:2024/06/16 17:23
原来最短路不难。自己老是觉得自己不会。。
虽然这道题我是看了别人的博客才写出来嘚。。

没想到这里真的就是一升一升地加。。居然不会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);        }    }}

0 0
原创粉丝点击