BZOJ_P2763 [JLOI2011]飛行路線(分層圖+最短路)

BZOJ傳送門

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1725 Solved: 647
[Submit][Status][Discuss]
Description

Alice和Bob現在要乘飛機旅行,他們選擇了一家相對便宜的航空公司。該航空公司一共在n個城市設有業務,設這些城市分別標記爲0到n-1,一共有m種航線,每種航線連接兩個城市,並且航線有一定的價格。Alice和Bob現在要從一個城市沿着航線到達另一個城市,途中可以進行轉機。航空公司對他們這次旅行也推出優惠,他們可以免費在最多k種航線上搭乘飛機。那麼Alice和Bob這次出行最少花費多少?

Input

數據的第一行有三個整數,n,m,k,分別表示城市數,航線數和免費乘坐次數。
第二行有兩個整數,s,t,分別表示他們出行的起點城市編號和終點城市編號。(0<=s,t< n)
接下來有m行,每行三個整數,a,b,c,表示存在一種航線,能從城市a到達城市b,或從城市b到達城市a,價格爲c。(0<=a,b< n,a與b不相等,0<=c<=1000)

Output

只有一行,包含一個整數,爲最少花費。

Sample Input

5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100

Sample Output

8

HINT

對於30%的數據,2<=n<=50,1<=m<=300,k=0;
對於50%的數據,2<=n<=600,1<=m<=6000,0<=k<=1;
對於100%的數據 ,2<=n<=10000,1<=m<=50000,0<=k<=10.

Source

Sol:
k辣麼小就分層
將j層連(j+1)層,費用爲0就可以了
直接跑最短路!據說SPFA會TLE?

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define N 110005
inline int in(int x=0,char ch=getchar()){while(ch>'9'||ch<'0') ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x;}
int n,m,k,s,t,ans;int d[N];bool b[N];
struct Edge{int to,w;};
vector<Edge> g[N];
inline void Add_Edge(int fr,int to,int w){g[fr].push_back((Edge){to,w});}
struct Heap{int to;long long d;bool operator < (const Heap &a)const{return d>a.d;}};
void Dijkstra(){
    priority_queue<Heap> q;q.push((Heap){s,0});
    memset(d,0x7f,sizeof(d));d[s]=0;int x;
    while(!q.empty()){
        x=q.top().to;q.pop();
        if(b[x]) continue;b[x]=1;
        for(int i=0,v,lim=g[x].size();i<lim;i++){
            if(d[x]+g[x][i].w<d[v=g[x][i].to]){
                d[v]=d[x]+g[x][i].w;
                q.push((Heap){v,d[v]});
            }
        }
    }
}
int main(){
    n=in(),m=in(),k=in();s=in(),t=in();int u,v,w;
    for(int i=1;i<=m;i++){
        u=in(),v=in(),w=in();
        for(int j=0;j<=k;j++){
            Add_Edge(j*n+u,j*n+v,w);Add_Edge(j*n+v,j*n+u,w);
            if(j<k) Add_Edge(j*n+u,(j+1)*n+v,0),Add_Edge(j*n+v,(j+1)*n+u,0);
        }
    }
    Dijkstra();ans=0x7fffffff;
    for(int i=0;i<=k;i++) ans=min(ans,d[i*n+t]);
    printf("%d\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章