SGU 103. Traffic Lights 帶限制最短路,sgutraffic

來源:互聯網
上載者:User

SGU 103. Traffic Lights 帶限制最短路,sgutraffic

每個點有2中顏色 只有一條路上的兩個點顏色一樣才能通過這條路 最短路加上等待的時間處理 處理的是參考別人的 唉還是太弱了

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;int s, e;int n, m;int a[333];int b[333];int c[333];int first[333], cnt;struct edge{int v, w, next;}G[30000];int d[333];int p[333];bool vis[333];struct HeapNode{int v, d;HeapNode(){}HeapNode(int v, int d): v(v), d(d){}bool operator < (const HeapNode& rhs) const{return d > rhs.d;}};void AddEdge(int u, int v, int w){G[cnt].v = v;G[cnt].w = w;G[cnt].next = first[u];first[u] = cnt++;G[cnt].v = u;G[cnt].w = w;G[cnt].next = first[v];first[v] = cnt++;}int wait(int t, int x, int y){int ans = 0;int t1 = (a[x] + t) % c[x];int t2 = (a[y] + t) % c[y];int tmp;for(int i = 0; i < 4; i++){if((t1 < b[x]) == (t2 < b[y]))return ans;if(t1 < b[x])tmp = b[x] - t1;elsetmp = c[x] - t1;if(t2 < b[y])tmp = min(tmp, b[y] - t2);elsetmp = min(tmp, c[y] - t2);t1 = (t1 + tmp) % c[x];t2 = (t2 + tmp) % c[y];ans += tmp;}return -1;}void print(int x){if(p[x] == -1){printf("%d", x);return;}print(p[x]);printf(" %d", x);}void Dijkstra(){for(int i = 1; i <= n; i++){d[i] = 999999999;p[i] = -1;vis[i] = false;}d[s] = 0;priority_queue <HeapNode> Q;Q.push(HeapNode(s, 0));while(!Q.empty()){HeapNode x = Q.top(); Q.pop();int u = x.v;if(vis[u])continue;vis[u] = 1;for(int i = first[u]; i != -1; i = G[i].next){int v = G[i].v;int x = d[u];int t = wait(x, u, v);if(t != -1 && d[v] > d[u] + G[i].w + t){d[v] = d[u] + G[i].w + t;p[v] = u;Q.push(HeapNode(v, d[v]));}}}if(d[e] != 999999999){printf("%d\n", d[e]);print(e);puts("");}elseputs("0");}int main(){memset(first, -1, sizeof(first));cnt = 0;scanf("%d %d", &s, &e);scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++){char s[10];int r, bl, pu;scanf("%s %d %d %d", s, &r, &bl, &pu);if(s[0] == 'B'){a[i] = bl-r;b[i] = bl;c[i] = bl+pu;}else{a[i] = bl+pu-r;b[i] = bl;c[i] = bl+pu;}}for(int i = 1; i <= m; i++){int u, v, w;scanf("%d %d %d", &u, &v, &w);AddEdge(u, v, w);}Dijkstra();return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.