您的位置:首页 > 其它

[kuangbin带你飞]专题四 最短路练习 MN

2016-12-21 14:08 218 查看
http://poj.org/problem?id=1062

题意:

中问题。。。

tip:

加上一个源点0,然后求0到1的最短路,不过这题唯一的限制是有一个等级限定,对于等级限定的话就直接用枚举等级区间来完成了。还有题目中说的“优惠价格”是优惠之后的价格。。。

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn =110;
int n,m,L,ll,rr;
int tot,inq[maxn],lev[maxn],dist[maxn],head[maxn];
queue<int>q;
struct node{
int v,w,next;
}edges[maxn*maxn];
void add(int u,int v,int w){
edges[tot].v = v;edges[tot].w = w;edges[tot].next = head[u];head[u] = tot++;
}
void init(){
tot = 0;
memset(head,-1,sizeof(head));
for(int i = 1; i <= m; i++){
int p,l,x,v,c;
scanf("%d%d%d",&p,&l,&x);
lev[i]=l;
if(i ==1)   L = l;
add(0,i,p);
for(int j = 0 ; j < x;j++){
scanf("%d%d",&v,&c);
add(v,i,c);
}
}
}

void spfa(int ll,int rr){
//printf("%d  rr - %d\n",ll,rr);
for(int i = 1 ; i <= m;i++) dist[i] = (1<<30);
memset(inq,0,sizeof(inq));
dist[0] = 0;
q.push(0);
inq[0] = 1;
while(!q.empty()){
int x=q.front();
//  printf("x = %d\n",x);
inq[x]=0;
q.pop();
for (int t=head[x];t!=-1;t=edges[t].next){
// printf(" to =     %d\n",edges[t].v);
if(lev[edges[t].v]<ll||lev[edges[t].v]>rr) continue;
// printf("dist[%d] =%d,dist[%d] = %d,edges = %d\n",edges[t].v,dist[edges[t].v],x,dist[x],edges[t].w);
if (dist[edges[t].v]>dist[x]+edges[t].w){
dist[edges[t].v]=dist[x]+edges[t].w;// printf("dist[%d]  = %d\n",edges[t].v,dist[edges[t].v]);
if (!inq[edges[t].v]) {
inq[edges[t].v]=1;
q.push(edges[t].v);
}
}
}
}
}

void sov(){
int minn = (1<<30);
for(int i =max(L-n,0);i<=L;i++){
spfa(i,i+n);
minn = min(minn,dist[1]);
}
printf("%d\n",minn);
}

int main(){
while(~scanf("%d%d",&n,&m)){
init();
sov();
}
}


N题转别人得

就是有n个交叉点,就当做有n个点就行,然后这些点和其他点有些路径,每个点是一个开关,开关只能有一个方向走一条路,而第一个数就是默认的开关指向,不用旋转。

这单犯了个错,就是默认的指向实际上只需要旋转0次,而其他路径只需要旋转1次,无论是哪条,只需1次,当初以为,第二个1次,第3个2次。

题目给的实例

3 2 1 //有3个开关点,计算从第二个到第一个最少需要旋转几次

2 2 3//第1个开关可以通向2 和3 ,通向2不需要旋转,通向3需要旋转1次

2 3 1//第2个开关可以通向3 和1, 通向3不需要转,通向次次

需要旋转1次 然后就直接做了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: