设为首页 加入收藏

TOP

POJ 3835 Columbus's bargain (最短路径 spfa 算法)(二)
2015-11-21 01:47:33 来源: 作者: 【 】 浏览:15
Tags:POJ 3835 Columbus' bargain 路径 spfa 算法
一条权值V-1的单向边)
3、物品价值相等 时物物交换 (将两个物品之间添一条权值为0的双向边)
4、物物价值不等时交换需要花C金币 (将A到B添一条权值为C的双向边)
?
按照构图,算一下单源最短路径
?
?
#include   
#include   
#include   
#include   
using namespace std;  
  
const int maxn=30;  
  
struct edge{  
    int u,v,w,next;  
    edge(int u0=0,int v0=0,int w0=0,int next0=0){  
        u=u0;v=v0;w=w0;next=next0;  
    }  
}e[maxn*maxn];  
  
int n,head[maxn],cnt,price[maxn],from,dist[maxn];  
bool visited[maxn];  
  
void initial(){  
    from=0;cnt=0;  
    for(int i=0;i<=n;i++){  
        visited[i]=false;  
        head[i]=-1;dist[i]=200000000;  
    }  
}  
  
void adde(int u,int v,int w){  
    e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt++;  
}  
  
void input(){  
    int m,x,y,z;  
    for(int i=0;i>x>>y;  
        price[x]=y;  
        adde(from,x,y-1);  
    }  
    for(int i=1;i<=n;i++){  
        for(int j=i+1;j<=n;j++){  
            if(price[i]==price[j]){  
                adde(i,j,0);  
                adde(j,i,0);  
            }  
        }  
    }  
    cin>>m;  
    while(m-- >0){  
        cin>>x>>y>>z;  
        adde(x,y,z);  
    }  
}  
  
void spfa(){  
    queue  q;  
    int s=from,d;  
    dist[from]=0;  
    q.push(s);  
    visited[s]=true;  
    while(!q.empty()){  
        s=q.front();  
        q.pop();  
        for(int i=head[s];i!=-1;i=e[i].next){  
            d=e[i].v;  
            if(dist[d]>dist[s]+e[i].w){  
                dist[d]=dist[s]+e[i].w;  
                if(!visited[d]){  
                    visited[d]=true;  
                    q.push(d);  
                }  
            }  
        }  
        visited[s]=false;  
    }  
      
}  
  
void computing(){  
    int ans=0;  
    spfa();  
    for(int i=1;i<=n;i++){  
        cout<>t;  
    while(t-- >0){  
        cin>>n;  
        initial();  
        input();  
        computing();  
    }  
    return 0;  
}  

?


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj - 3683 - Priest John's .. 下一篇hdu4352 XHXJ's LIS

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: