URAL - 1741 Communication Fiend
来源:互联网 发布:2016 fc2视频最新域名 编辑:程序博客网 时间:2024/06/04 18:57
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#include<queue>using namespace std;const int N = 10100;typedef long long _int;struct edge{ int v,w,f;};//f -1 pirated 0 cracked 1 licensed//dis[][0] the road is pirated//dis[][1] the road is licensedvector<edge> g[N];queue<int> q;bool inq[N];const _int inf = ((_int)1<<50);_int dis[N][2];int n;void update(_int &a,_int &b,_int &c,int v){ a=b+c; if(!inq[v]){ inq[v]=true; q.push(v); }}void work(){ for(int i=2;i<=n;i++) dis[i][1]=dis[i][0]=inf; dis[1][1]=0; dis[1][0]=inf; memset(inq,0,sizeof(inq)); q.push(1); while(!q.empty()){ int x=q.front(); q.pop(); inq[x]=false; int sz=g[x].size(); for(int i=0;i<sz;i++){ int v=g[x][i].v; _int w=g[x][i].w; int f=g[x][i].f; if(f==-1){ if(dis[v][0]>dis[x][0]+w) update(dis[v][0],dis[x][0],w,v); if(dis[v][0]>dis[x][1]+w) update(dis[v][0],dis[x][1],w,v); } else if(f==0){ if(dis[v][0]>dis[x][0]+w) update(dis[v][0],dis[x][0],w,v); if(dis[v][1]>dis[x][1]+w) update(dis[v][1],dis[x][1],w,v); } else if(f==1) if(dis[v][1]>dis[x][1]+w) update(dis[v][1],dis[x][1],w,v); } }}int main(){ int m; while(~scanf("%d%d",&n,&m)){ int u,v; _int w; char str[20]; for(int i=1;i<=n;i++) g[i].clear(); for(int i=0;i<m;i++){ scanf("%d%d%I64d%s",&u,&v,&w,str); edge t; t.v=v,t.w=w; if(str[0]=='L') t.f=1; else if(str[0]=='P') t.f=-1; else if(str[0]=='C') t.f=0; g[u].push_back(t); } work(); _int ans=min(dis[n][0],dis[n][1]); if(ans==inf){ printf("Offline\n"); continue; } else printf("Online\n%I64d\n",ans); } return 0;}