比赛 |
练习12 |
评测结果 |
AAAAAAAAAAAAA |
题目名称 |
架设电话线 |
最终得分 |
100 |
用户昵称 |
NVIDIA |
运行时间 |
0.012 s |
代码语言 |
C++ |
内存使用 |
3.42 MiB |
提交时间 |
2017-06-30 13:03:07 |
显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxl=1e10+5;
int n,m,k,i,j,c,head[1010],tail[1010],next[200010],ans;
inline int read()
{
int x,f=1;char ch;
while(ch=getchar(),!isdigit(ch))if(ch=='-')f=-1;x=ch-48;
while(ch=getchar(),isdigit(ch))x=x*10+ch-48;return x*f;
}
/*char buf[1<<15],*fs,*ft;
inline char getc() {return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;}
inline int q()
{
int x=0,f=1; char ch=getc();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getc();}
while(isdigit(ch)){x=x*10+ch-'0';ch=getc();}return x*f;
}
char mas[1<<15],*FS=mas,*FT=mas+(1<<15);
#define ot(x) (fs==ft?fwrite(mas,1,1<<15,stdout),*(FS=mas)++=x:*FS++=x)
inline void w(int x)
{
if(x<0) {ot(45);x=-x;}
static char s[15],*b;b=s;
if(!x)*b++=48;
for(;x;*b++=x%10+48,x/=10);
for(;b--!=s;ot(*b));
}*/
struct edge
{
int f,t,l;
}w[200010];
int heap[1000010],top,hash[1000010];
int dist[1000010],v;//dist[i+k*n]表示到第i个点,用k条0长边的最短路长
inline bool cmp(int x,int y){
return dist[heap[x]]<dist[heap[y]];
}
inline void swap(int x,int y){
int a=heap[x];
heap[x]=heap[y];heap[y]=a;
hash[heap[x]]=x;hash[heap[y]]=y;
}
inline void insert(int x){
int fa,son=++top;
heap[son]=x;hash[x]=son;
while (son>1){
fa=son/2;
if (cmp(fa,son)) break;
swap(fa,son);
son=fa;
}
}
inline void maintain(int x){
int fa,son=hash[x];
while (son>1){
fa=son/2;
if (cmp(fa,son)) break;
swap(fa,son);
son=fa;
}
}
inline void pop(){
hash[heap[1]]=0;
heap[1]=heap[top--];
hash[heap[1]]=1;
int fa=1,son;
while (fa<=top){
son=fa*2;
if (son>top) break;
if (son+1<=top&&cmp(son+1,son)) son++;
if (cmp(fa,son)) break;
swap(fa,son);
fa=son;
}
}
inline void dijsktra(){
for (i=1;i<=(k+1)*n;i++) dist[i]=maxl;
dist[1]=0;insert(1);
while (top>0){
v=heap[1];pop();
c=(v-1)/n;
j=v-c*n;
if (j==n) return;
for (i=head[j];i!=0;i=next[i]){
if (dist[w[i].t+c*n]>max(dist[v],w[i].l)){
dist[w[i].t+c*n]=max(dist[v],w[i].l);
if (hash[w[i].t+c*n]==0) insert(w[i].t+c*n);
else maintain(w[i].t+c*n);
}
if (c<k&&dist[w[i].t+c*n+n]>dist[v]){
dist[w[i].t+c*n+n]=dist[v];
if (hash[w[i].t+c*n+n]==0) insert(w[i].t+c*n+n);
else maintain(w[i].t+c*n+n);
}
}
}
}
inline int WOAKC()
{
freopen("phoneline.in","r",stdin);
freopen("phoneline.out","w",stdout);
n=read();m=read();k=read();
for (i=1;i<=m;i++){
scanf("%d%d%d",&w[i].f,&w[i].t,&w[i].l);
w[i+m].f=w[i].t;
w[i+m].t=w[i].f;
w[i+m].l=w[i].l;
if (head[w[i].f]==0) head[w[i].f]=tail[w[i].f]=i;
else tail[w[i].f]=next[tail[w[i].f]]=i;
if (head[w[i].t]==0) head[w[i].t]=tail[w[i].t]=i+m;
else tail[w[i].t]=next[tail[w[i].t]]=i+m;
}
dijsktra();
ans=maxl;
for (i=0;i<=k;i++) ans=min(ans,dist[n+n*i]);
printf("%d\n",(ans==maxl?-1:ans));
return 0;
}
int WO=WOAKC();
int main(){;}
/*
驴蛋大佬的0s写法
#include <cstdio>
#include <cstring>
#include <queue>
const int MAXNODE=1010;
const int MAXPATH=10010;
const int INF=0x7fffff;
int Rec,n,m;
inline int min(const int&a,const int&b){return a<b?a:b;}
inline int max(const int&a,const int &b){return a<b?b:a;}
struct Path{
int to,dis,next;
Path(){to=dis=next=-1;}
};
struct Node{
int pos,dis;
Node(const int&a,const int&b){pos=a;dis=b;}
};
bool operator < (const Node&a,const Node&b){return a.dis>b.dis;}
struct Pic{
int lens;
int head[MAXNODE];
int dis[MAXNODE];
Path table[MAXPATH*2];
Pic(){memset(head,-1,sizeof(head));lens=0;}
void add(const int&from,const int&to,const int&dis){
table[++lens].next=head[from];
table[lens].to=to;
table[lens].dis=dis;
head[from]=lens;
}
bool dijsktra(const int&from,const int&to,const int&up_load){
std::priority_queue<Node>q;
memset(dis,63,sizeof(dis));
q.push(Node(from, dis[from]=0));
while(!q.empty()){
Node p=q.top();q.pop();//printf("dis->%d\n",p.dis);
if(dis[p.pos]!=p.dis)continue;
for(int i=head[p.pos];i!=-1;i=table[i].next){
if((table[i].dis>up_load)+dis[p.pos]<=Rec){
if((table[i].dis>up_load)+dis[p.pos]<dis[table[i].to]){
dis[table[i].to]=(table[i].dis>up_load)+dis[p.pos];
q.push(Node(table[i].to,dis[table[i].to]));
}
}
}
}return dis[0]!=dis[to];
}
}pic;
int doing(){
freopen("phoneline.in","r",stdin);freopen("phoneline.out","w",stdout);
int l=0,r=0,mid,p;
scanf("%d%d%d",&n,&m,&Rec);
for(int x,y,z,i=1;i<=m;++i){
scanf("%d%d%d",&x,&y,&z);
pic.add(x,y,z);
pic.add(y,x,z);
r=max(r,z);
}p=r;
while(l<=r){
mid=((l+r)>>1);
if(pic.dijsktra(1,n,mid))r=mid-1;
else l=mid+1;
}if(l>p)l=-1;
printf("%d",l);
//while(1);
return 0;
}
*/