zoukankan      html  css  js  c++  java
  • [BZOJ2763] [JLOI2011] 飞行路线

    题目链接

    BZOJ.

    洛谷.

    Solution

    这玩意叫分层图最短路吗?...

    一个点拆成(k)个,点(p(x,i))表示到点(x),用了(i)次免费机会。

    那么一条有向边可以拆成(2k)条边:

    • 可以选择不用免费机会,那么对于每个(i),连边(p(u,i) o p(v,i)),边权为(w)
    • 否则对于每个(i),连边(p(u,i) o p(v,i+1)),边权为(0)

    然后普通的跑最短路就行了。

    #include<bits/stdc++.h>
    using namespace std;
    
    void read(int &x) {
        x=0;int f=1;char ch=getchar();
        for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
        for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
    }
    
    void print(int x) {
        if(x<0) putchar('-'),x=-x;
        if(!x) return ;print(x/10),putchar(x%10+48);
    }
    void write(int x) {if(!x) putchar('0');else print(x);putchar('
    ');}
    
    #define lf double
    #define ll long long 
    
    const int maxn = 2e6+10;
    const int inf = 1e9;
    const lf eps = 1e-8;
    
    int n,m,k,head[maxn],tot,s,t,dis[maxn];
    struct edge{int to,nxt,w;}e[maxn<<1];
    
    inline int p(int x,int y) {return y*n+x;}
    
    void ins(int u,int v,int w) {e[++tot]=(edge){v,head[u],w},head[u]=tot;}
    
    #define fr first
    #define sc second
    #define mp make_pair
    
    void dijkstra() {
    	priority_queue<pair<int,int > > q;
    	memset(dis,63,sizeof dis);
    	q.push(mp(dis[s]=0,s));
    	while(!q.empty()) {
    		int now=q.top().sc,d=-q.top().fr;q.pop();
    		if(dis[now]<d) continue;
    		for(int i=head[now];i;i=e[i].nxt) 
    			if(dis[e[i].to]>dis[now]+e[i].w) {
    				dis[e[i].to]=dis[now]+e[i].w;
    				q.push(mp(-dis[e[i].to],e[i].to));
    			}
    	}
    }
    
    int main() {
    	read(n),read(m),read(k),read(s),read(t),s++,t++;
    	for(int w=1,x,y,z;w<=m;w++) {
    		read(x),read(y),read(z);x++,y++;
    		for(int i=0;i<=k;i++) ins(p(x,i),p(y,i),z);
    		for(int i=0;i<k;i++) ins(p(x,i),p(y,i+1),0);
    		swap(x,y);
    		for(int i=0;i<=k;i++) ins(p(x,i),p(y,i),z);
    		for(int i=0;i<k;i++) ins(p(x,i),p(y,i+1),0);
    	}
    	for(int i=0;i<k;i++) ins(p(t,i),p(t,k),0);  //为了防止坑爹数据加上这句话
    	dijkstra();
    	write(dis[p(t,k)]);
    	return 0;
    }
    
  • 相关阅读:
    python配置apache的web服务器方法(python的CGI配置)
    【转】移动web资源整理
    CSS实现背景透明,文字不透明,兼容所有浏览器
    html5 css3 如何绘制扇形任意角度
    Chrome 将默认不播放非重要 Flash 内容
    微信video标签全屏无法退出bug
    百度bae定时任务使用方法
    判断浏览器是否支持某个css3属性的javascript方法
    javascript检测是否安装了flash
    移动前端不得不了解的html5 head 头标签
  • 原文地址:https://www.cnblogs.com/hbyer/p/10632981.html
Copyright © 2011-2022 走看看