(spfa專題 1.1)hdu 1535 Invitation Cards(求從1到2~n的點後,再從2~n返回1點的最小距離),spfahdu

來源:互聯網
上載者:User

(spfa專題 1.1)hdu 1535 Invitation Cards(求從1到2~n的點後,再從2~n返回1點的最小距離),spfahdu

題目:

Invitation CardsTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2350    Accepted Submission(s): 1141


Problem DescriptionIn the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 

 
InputThe input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop. 
 
OutputFor each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers. 
 
Sample Input

22 21 2 132 1 334 61 2 102 1 601 3 203 4 102 4 54 1 50
 
Sample Output
46210
 
SourceCentral Europe 1998 
RecommendLL   |   We have carefully selected several similar problems for you:  1317 1217 1531 1546 1384 


題目分析:

              這道題採用spfa來做。有以下2中思路:

       1)首先以1為源點進行一次spfa()演算法,求得1到2~n點的最短距離。接下來就是分別以2~n點位源點,進行spfa(),這樣既可得到答案。但是這就得進行n次spfa()演算法了,在這種資料規模下(節點數n最大可達1000000),很有可能會TLE。

      2)反向建邊。首先以1位源點進行一次spfa演算法(),求得1到2~n各個節點的最短距離。然後反向建邊,再以1位源點進行一次spfa()演算法,這時其實求得的是2~n到1的最短距離。(這道題是有向圖,不是無向圖)。


以下分別貼上了自己寫的三個版本,雖然後面兩個這道題沒能AC。但是基本體現了用各種資料結構實現的spfa演算法的

基本思路及基本使用。



AC代碼如下(spfa,用隊列+鄰接表的實現):


代碼如下:

/* * hdu_1535_1.cpp * *  Created on: 2015年3月27日 *      Author: Administrator */#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int SIZE = 1000001;const int INF = 0x3f3f3f3f;int u[2 * SIZE], v[2 * SIZE], w[2 * SIZE], nextt[2 * SIZE];int startss[2 * SIZE], endss[2 * SIZE], weightss[2 * SIZE];int head[SIZE], dist[SIZE];int n, m, cnt;/** * 初始化 */void init() {memset(u, 0, sizeof(u));memset(v, 0, sizeof(v));memset(w, INF, sizeof(w));memset(dist, 0, sizeof(dist));memset(nextt, 0, sizeof(nextt));memset(head, -1, sizeof(head));cnt = 0;}/** * 用隊列實現的spfa。 * * 參數說明: * src: 起點(源點) */void spfa(int src) {queue<int> q;bool inq[SIZE] = { 0 };for (int i = 1; i <= n; i++)dist[i] = (i == src) ? 0 : INF;q.push(src);while (!q.empty()) {int x = q.front();q.pop();inq[x] = 0;for (int e = head[x]; e != -1; e = nextt[e])if (dist[v[e]] > dist[x] + w[e]) {dist[v[e]] = dist[x] + w[e];if (!inq[v[e]]) {inq[v[e]] = 1;q.push(v[e]);}}}}/** * 添加邊 */void add_edge(int u1, int v1, int w1) {u[cnt] = u1;v[cnt] = v1;w[cnt] = w1;nextt[cnt] = head[u[cnt]];head[u[cnt]] = cnt;cnt++;}int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);int i;for (i = 0; i < m; ++i) {scanf("%d%d%d", &startss[i], &endss[i], &weightss[i]);}/** * 使用spfa演算法的基本步驟 */init();//初始化for (i = 0; i < m; ++i) {//讀入資料add_edge(startss[i], endss[i], weightss[i]);}spfa(1);//進行運算int ans = 0;for(i = 2 ; i <= n ; ++i){//計算1到各個節點的最短距離ans += dist[i];}init();for(i = 0 ; i < m ; ++i){//這裡反向建邊add_edge(endss[i],startss[i],weightss[i]);}spfa(1);for(i = 2 ; i <= n ; ++i){//計算回程時各個節點到1的最短距離ans += dist[i];}printf("%d\n",ans);//輸出最後的結果..}return 0;}


StackOverFlow的版本:

spfa(用棧+鄰接表)

/* * hdu_1535.cpp * *  Created on: 2015年3月27日 *      Author: Administrator */#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int INF = 0x3F3F3F3F;const int V = 1000010;const int E = 1000010;int pnt[4*E], cost[4*E], nxt[4*E];int e, head[V];int dist[V];bool vis[V];int startss[4*E];int endss[4*E];int weightss[4*E];int relax(int u, int v, int c) {if (dist[v] > dist[u] + c) {dist[v] = dist[u] + c;return 1;}return 0;}inline void addedge(int u, int v, int c) {pnt[e] = v;cost[e] = c;nxt[e] = head[u];head[u] = e++;}/** * 使用 鄰接表+棧 實現的spfa * * 參數說明: * src: 源點 * n: 節點的個數 */int SPFA(int src, int n) { //  此處用堆棧實現,有些時候比隊列要快int i;for (i = 1; i <= n; ++i) { //  頂點1...nvis[i] = 0;dist[i] = INF;}dist[src] = 0;int Q[E], top = 1;Q[0] = src;vis[src] = true;while (top) {int u, v;u = Q[--top];vis[u] = false;for (i = head[u]; i != -1; i = nxt[i]) {v = pnt[i];if (1 == relax(u, v, cost[i]) && !vis[v]) {Q[top++] = v;vis[v] = true;}}}return dist[n];}int main() {int t;scanf("%d", &t);while (t--) {int n, m;scanf("%d%d", &n, &m);memset(head, -1, sizeof(head));e = 0;int i;for (i = 0; i < m; ++i) {scanf("%d%d%d", &startss[i], &endss[i], &weightss[i]);}for(i = 0 ; i < m ; ++i){addedge(startss[i],endss[i],weightss[i]);}int ans = 0;SPFA(1, n);for (i = 2; i <= n; ++i) {ans += dist[i];}memset(head, -1, sizeof(head));e = 0;for(i = 0 ; i < m ; ++i){addedge(endss[i],startss[i],weightss[i]);}SPFA(1,n);for(i = 2 ; i <= n ; ++i){ans += dist[i];}printf("%d\n",ans);}return 0;}


CE的版本:

spfa演算法(隊列+鄰接矩陣)

CE的原因估計是:二位矩陣開得太大了。

/* * hdu1535.cpp * *  Created on: 2015年3月27日 *      Author: Administrator */#include <iostream>#include <cstring>#include <queue>using namespace std;const int N = 1000001;const int INF = 0x3f3f3f3f;int map[N][N], dist[N];bool visit[N];int n, m;int startss[N];int endss[N];int weightss[N];void init() {//初始化int i, j;for (i = 1; i < N; i++) {for (j = 1; j < N; j++) {if (i == j) {map[i][j] = 0;} else {map[i][j] = map[j][i] = INF;}}}}/** * SPFA演算法. * 使用spfa演算法來求單元最短路徑 * 參數說明: * start:起點 */void spfa(int start) {queue<int> Q;int i, now;memset(visit, false, sizeof(visit));for (i = 1; i <= n; i++){dist[i] = INF;}dist[start] = 0;Q.push(start);visit[start] = true;while (!Q.empty()) {now = Q.front();Q.pop();visit[now] = false;for (i = 1; i <= n; i++) {if (dist[i] > dist[now] + map[now][i]) {dist[i] = dist[now] + map[now][i];if (visit[i] == 0) {Q.push(i);visit[i] = true;}}}}}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);init();int i;for(i = 0 ; i < m ; ++i){scanf("%d%d%d",&startss[i],&endss[i],&weightss[i]);}int ans = 0;for(i = 0 ; i < m ; ++i){map[startss[i]][endss[i]] = weightss[i];}spfa(1);for(i = 2 ; i <= n ; ++i){ans += dist[i];}init();for(i = 0 ; i < m ; ++i){map[endss[i]][startss[i]] = weightss[i];}spfa(1);for(i = 2 ; i <= n ; ++i){ans += dist[i];}printf("%d\n",ans);}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.