CodeForcesGym 100753A A Journey to Greece

來源:互聯網
上載者:User

標籤:

A Journey to GreeceTime Limit: 5000msMemory Limit: 262144KBThis problem will be judged on CodeForcesGym. Original ID: 100753A
64-bit integer IO format: %I64d      Java class name: (Any)
 解題:狀壓dp
 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef pair<int,int> pii; 4 const int maxn = 20010; 5 struct arc { 6     int to,cost,next; 7     arc(int x = 0,int y = 0,int z = -1) { 8         to = x; 9         cost = y;10         next = z;11     }12 } e[maxn*50];13 int head[maxn],hs[maxn],tot,N,P,M,G,T;14 void add(int u,int v,int cost) {15     e[tot] = arc(v,cost,head[u]);16     head[u] = tot++;17 }18 priority_queue<pii,vector< pii >,greater< pii > >q;19 int d[20][maxn];20 bool done[maxn];21 void dijkstra(int S) {22     while(!q.empty()) q.pop();23     int s = hs[S];24     memset(d[s],0x3f,sizeof d[s]);25     q.push(pii(d[s][S] = 0,S));26     memset(done,false,sizeof done);27     while(!q.empty()) {28         int u = q.top().second;29         q.pop();30         if(done[u]) continue;31         done[u] = true;32         for(int i = head[u]; ~i; i = e[i].next) {33             if(d[s][e[i].to] > d[s][u] + e[i].cost) {34                 d[s][e[i].to] = d[s][u] + e[i].cost;35                 q.push(pii(d[s][e[i].to],e[i].to));36             }37         }38     }39 }40 int city[20],ttime[20],dp[1<<20][20][2];41 int main() {42     int u,v,w;43     while(~scanf("%d%d%d%d%d",&N,&P,&M,&G,&T)) {44         memset(head,-1,sizeof head);45         bool zero = false;46         for(int i = tot = 0; i < P; ++i) {47             scanf("%d%d",city + i,ttime + i);48             hs[city[i]] = i;49             if(city[i] == 0) zero = true;50         }51         for(int i = 0; i < M; ++i) {52             scanf("%d%d%d",&u,&v,&w);53             add(u,v,w);54             add(v,u,w);55         }56         for(int i = 0; i < P; ++i) dijkstra(city[i]);57         if(!zero){58             hs[0] = P;59             dijkstra(0);60         }61         memset(dp,0x3f,sizeof dp);62         for(int i = 0; i < P; ++i){63             dp[1<<i][i][1] = d[hs[0]][city[i]] + ttime[i];64             dp[1<<i][i][0] = T + ttime[i];65         }66         int st = (1<<P);67         for(int i = 1; i < st; ++i){68             for(int j = 0; j < P; ++j){69                 if(((i>>j)&1) == 0) continue;70                 for(int k = 0; k < P; ++k){71                     if((i>>k)&1) continue;72                     dp[i|(1<<k)][k][1] = min(dp[i|(1<<k)][k][1],dp[i][j][1] + d[j][city[k]] + ttime[k]);73                     dp[i|(1<<k)][k][0] = min(dp[i|(1<<k)][k][0],dp[i][j][1] + T + ttime[k]);74                     dp[i|(1<<k)][k][0] = min(dp[i|(1<<k)][k][0],dp[i][j][0] + d[j][city[k]] + ttime[k]);75                 }76             }77         }78         bool wtx = false,poss = false;79         for(int i = 0; i < P && !wtx; ++i){80             if(dp[st-1][i][1] + d[i][0] <= G) wtx = true;81             if(dp[st-1][i][1] + T <= G) poss = true;82             if(dp[st-1][i][0] + d[i][0] <= G) poss = true;83         }84         if(wtx) puts("possible without taxi");85         else if(poss) puts("possible with taxi");86         else puts("impossible");87     }88     return 0;89 }
View Code

 

CodeForcesGym 100753A A Journey to Greece

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.