您的位置:首页 > 其它

Full Tank? UVA - 11367

2017-04-22 12:32 330 查看
原来最短路不难。自己老是觉得自己不会。。
虽然这道题我是看了别人的博客才写出来嘚。。

没想到这里真的就是一升一升地加。。居然不会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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: